[C/C ++ multi-thread programming 4] terminating the pthread thread

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.