Pages

Joining a POSIX thread

Once you create a new thread on a specified function, your original main thread is free to do whatever it likes. It could even terminate the program execution - and that would be no fun for the other thread, since it would like to have its time to do its job, too.

So, usually it is a good idea to wait until the spawned threads complete their life before terminating. To do that, in term of POSIX API, we use the pthread_join() function.Referring to the previous post, where we created the new thread like this:
pthread_t thread;

pthread_create(&thread, /* ... */);
We can now wait that the other thread completes its run calling:
pthread_join(thread, NULL);
The second parameter, when not NULL, would refer to the memory location where the value returned by the function on which that thread runs should be stored.

This function too returns zero if the specified thread joins happily, otherwise a non-zero value showing something bad happened.

No comments:

Post a Comment