0

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; 
}

3 Answers 3

1

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");
  }
4
  • so if I put pthread_exit() out of the loop, will there 6 threads (3 threads/loop * 2 times )created in my case? or I still get 3 threads because the second loop will use the same pthread_t ?
    – KennyYang
    Oct 31, 2015 at 6:59
  • @KennyYang: pthread_create() will be called three times by two iterations, that is six times. The second iteration however will not perform before the three threads created first had been joined, that is ended.
    – alk
    Oct 31, 2015 at 7:01
  • Is it correct that the second iteration use the same address of the first iteration thread so that the second one will wait until the first one stop..?
    – KennyYang
    Oct 31, 2015 at 7:08
  • @KennyYang The second iteration only is performed after all three calls to pthread_join() returned. pthread_join() blocks until the thread the pthread-id used ended. Due to this, yes, the second set of three threads will only be created after the first set of three threads ended.
    – alk
    Oct 31, 2015 at 7:12
1

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.

4
  • 1
    That answer is wrong. Sure you may call pthread_exit from main, and there is even a special rule in place if you do so. Oct 31, 2015 at 6:41
  • @JensGustedt Ok maybe it's technically allowed, but why on earth would you want to do that? Clearly the original poster was not intending to put it in main().
    – kcraigie
    Oct 31, 2015 at 6:45
  • 1
    You do that if your threads themselves create new threads and you have no centralized place that keeps track of all thread IDs. Then you can't join on all threads simply because you don't know them. Think of a dynamically maintained thread pool for example. In such a situation you have to take care that main, when it ends, doesn't kill the process and all other threads with it. Using pthread_exit is the way to do this. Oct 31, 2015 at 6:51
  • @JensGustedt Thanks, I didn't know that. I'll edit my answer accordingly.
    – kcraigie
    Oct 31, 2015 at 6:52
0

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 ?.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.