Search code examples
c++cpthreadspthread-join

What if thread exits before calling pthread_join


I have a small code

void *PrintHello(void *threadid)
{
   cout<<"Hello"<<endl;
   pthread_exit(NULL);
}

int main ()
{
   pthread_t threads_id;
   pthread_create(&threads_id, NULL, PrintHello, NULL);
   int i=0;
   for(;i<100;i++){cout<<"Hi"<<endl;}   
   pthread_join(threads_id,NULL);
   return 0;
}

I am joining the thread sometime after creation. What will happen if the main tries to join a thread which already exited?


Solution

  • What will happen if the main tries to join a thread which already exited?

    The join operation will immediately finish and return.