简体   繁体   中英

pthread_exit confusion in for loop

I tried to create 3 threads to do things and repeat for 2 times, I have expected that all of the threads will exit by using pthread_exit(NULL) , but it seems that the output only shows one time and maybe the threads only created one time ...?

I am confused of the usage of pthread_exit() .

Is it correct that I can destroy the thread by using this one ...?

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <sched.h>

#define gettid() syscall(__NR_gettid)

void *f1(void *a)
{
    printf("f1\n");
    return NULL;
}

void *f2(void *a)
{
    printf("f2\n");
    return NULL;
}

int main(int argc, char const *argv[])
{
    /* code */
    pthread_t t0,t1,t2; 
    int i ;

    for ( i = 0 ; i < 2 ; i++)
    {
      if (pthread_create(&t0,NULL,&f1,NULL)== -1)
        printf("error 1\n");
      if (pthread_create(&t1,NULL,&f2,NULL)== -1)
        printf("error 2\n");
      if (pthread_create(&t2,NULL,&f1,NULL)== -1)
        printf("error 1\n");

      pthread_join(t0,NULL);
      pthread_join(t1,NULL);
      pthread_join(t2,NULL);

      pthread_exit(NULL);
    }

    return 0; 
}

You probably don't want to call pthread_exit() where you are calling it, because that will cause your main thread to exit but allow your other threads to continue running. In this case you wait for all your other threads to finish, then exit your main thread. Thus your loop never gets to execute a second time.

the output only shows one time and maybe the threads only created one time

The code calls pthread_exit() after the 1st iteration in main()

  for (i = 0 ; i < 2 ; i++)
  {
    ...
    pthread_exit(NULL);
  }

so the thread represented by main() exits and no further iteration are done, so each call to pthread_create() is done once only.

To fix this move the call to pthread_exit() after the for -loop:

  for (i = 0 ; i < 2 ; i++)
  {
    ...
  }

  pthread_exit(NULL);

I am confused of the usage of pthread_exit() .

Is it correct that I can destroy the thread by using this one..?

pthread_exit() ends the calling thread.

The resources allocated to the ended thread are freed depending whether the thread is detached or attached , the latter is the default.

A thread's resources are freed, for a thread in state ...

  • attached by calling pthread_join() upon the related pthread-id.
  • detached upon the thread's termination, so no call to pthread_join() is necessary.

To detach a thread use pthread_detach() on the pthread-id of the thread to be detached.


Also please be aware that for recent implementations of the PThread-API all functions return an error code > 0 .

So you should change

  if (pthread_create(&t0, NULL,&f1, NULL)== -1)
    printf("error 1\n");

to be

  if (0 != pthread_create(&t0, NULL,&f1, NULL))
  {
    printf("error 1\n");
  }

or even better

  int result;

  ...

  if (0 != (result = pthread_create(&t0, NULL, &f1, NULL)))
  {
    errno = result;
    perror("pthread_create() failed");
  }

In your case since the main calls it, main thread will terminate.

The pthread_exit() exit the thread by which it call. You need to see when-to-use-pthread-exit ? .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM