[Original] Teach you how to design multiple threads in Linux-multi-thread programming in Linux (2)

Source: Internet
Author: User

This article can be reproduced at will, but the author and source must be indicated.

[Original] Teach you how to design multiple threads in Linux (1)
-- Multi-thread programming in Linux Original Author: frozen_socker (popsicle) E_mail: dlskyfly@163.com

Next, let's look at the other two important functions pthread_exit and pthread_join.

The function prototype is as follows:

Void pthread_exit (void * value_ptr );

 

 

The termination of a thread can be a call to pthread_exit or the end of the thread's routine. That is to say, a thread can exit implicitly or explicitly call the pthread_exit function.

 

 

The unique parameter value_ptr of the pthread_exit function is the return code of the function. As long as the second parameter value_ptr in pthread_join is not null, this value is passed to value_ptr.

The function prototype is as follows:

Int pthread_join (pthread_t thread, void ** value_ptr );

 

The function pthread_join is used to wait for a thread to terminate.

 

 

The thread that calls pthread_join will be suspended until the end of the thread represented by the parameter thread. Pthread_join is a thread blocking function. The function called by pthread_join will wait until the end of the waiting thread.

If value_ptr is not null, the return value of the thread is stored at the position pointed to by the pointer. The returned value can be the value given by pthread_exit, or pthread_canceled is returned if the thread is canceled.

 

 
When a non-detached thread is terminated, its memory resources (thread Descriptor and stack) will not be released until a thread uses pthread_join for it. Therefore, a pthread_join call must be called for each non-detached thread created to avoid Memory leakage. Otherwise, when the thread is detachable, calling pthread_exit will terminate the call thread and release all resources without waiting for it to terminate.

 

 

At most one thread can wait for the specified thread to terminate. If a thread is already waiting for the thread to terminate, calling pthread_join again to wait for the same thread will return an error.

// Example_2.c
# Include <stdio. h>
# Include <pthread. h>

Void * pthread_func_test (void * Arg );

Int main ()
...{
Pthread_t pt1, pt2;
Pthread_create (& pt1, null, pthread_func_test, "this is the thread_one ");
Pthread_create (& pt2, null, pthread_func_test, "this is the thread_two ");
Pthread_join (pt1, null );
Pthread_join (pt2, null); // What happens if this row is not written? Or write pthread_join (pt1, null); what will happen?
}

Void * pthread_func_test (void * Arg)
...{
Printf ("% s", ARG );
Pthread_exit (null); // explicit declaration
}

 

So far, we have learned three important functions: Create, exit, and join. Next, we will continue to explain the thread mutex problem in multi-thread programming.

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.