Termination of the pthread for multithreaded programming
PthreadPOSIX threads is short for POSIXThread Standard.Terminating a thread seems to be the last step of multi-threaded programming, but it is not the end of this series of teaching materials. When a thread is created to terminate the thread, we hope to give readers an overall understanding of multi-threaded programming.1. Terminate the Pthread: pthread_exit ()
Parameters:Ret: Address pointer, which is essentially the address written by the return value.Terminating a thread is an active action of a thread. A thread calls pthread_exit to terminate the thread itself. When a thread is terminated, specific data of the thread is released. The specific data of the thread is dedicated to the thread. Because the thread shares global data, the global data of the process is not released when the thread exits.How to Use the returned value ret of this function? The returned value is the call concept. Therefore, the returned value ret takes effect only when one thread is "called" by another thread. "Call" is different from the function call in the general sense. A thread waits for a thread and can be understood as "call ". Such as a thread callPthread_join waits for another thread to terminate. The following describes the wait thread termination function pthread_join.2. Waiting for thread termination: pthread_join ()The returned value ret is passed through another function pthread_join. The prototype of pthread_join waiting for thread termination is:
Waiting for the thread to terminate pthread_join will block the calling thread until the specified thread ends. Pthread_join specifies the thread by the first parameter: thread ID. The caller calls pthread_jion to wait for a specific thread to terminate. In this case, the caller may need the returned value of this specific thread. pthread_join assigns the value_ptr address to the ret of pthread_exit of the specific thread to obtain the returned value.3. Test with pthread_exi and pthread_join:In the preceding example, the main thread main calls pthread_join to wait for the sub-thread My_thread to terminate. It passes the My_thread_ret address to obtain the return value of the sub-thread My_thread and outputs the returned value on the screen.