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?
If you want to be sure that your thread have actually finished, you want to call pthread_join . If you don't, then terminating your program will terminate all the unfinished thread abruptly.
The pthread_join() function waits for a thread to terminate, detaches the thread, then returns the threads exit status. If the status parameter is NULL, the threads exit status is not returned.
The pthread_join() function provides a simple mechanism allowing an application to wait for a thread to terminate. After the thread terminates, the application may then choose to clean up resources that were used by the thread.
Return Values pthread_join() returns zero when it completes successfully.
What will happen if the main tries to join a thread which already exited?
The join operation will immediately finish and return.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With