(Note that if you have already detached the thread using pthread_detach(), you cannot join it again using pthread_join().)īoth approaches can be very tricky, but either might be specially useful in a given situation.
These last two calls might mess with the lexical scope of the code (since they may be macros yielding tokens) and are very difficult to maintain properly.
The pthreadjoin() function provides a simple mechanism allowing an application to wait for a thread to terminate. Thus, while not a primitive, including pthreadjoin() in this volume of POSIX.1-2017 was considered valuable. This approach requires detailed usage of pthread_cleanup_push() and pthread_cleanup_pop() to avoid resource leakage. I use pthreadcreate create the thread in C code, then use pthreadjoin or return to exit the thread(pthreadexit() throw the unwind error ), then i find the. Also, including the pthreadjoin() function in no way precludes a programmer from coding such complex waits. When the thread must terminate, execute pthread_cancel() and wait for its termination with pthread_join(). Use a cancellation point: The thread terminates whenever a cancellation function is executed.
This approach requires pre-synchronization between the parent thread and the child thread, to guarantee that the child thread has already installed the signal handler before it is able to handle the termination signal When the thread must terminate, issue the signal to it using pthread_kill() and wait for its termination with pthread_join().
Use a signal: The thread installs a signal handler using sigaction() which sets a flag, and the thread periodically checks the flag to see whether it must terminate.There are two approaches to this problem.