Linux multithreading, separation and integration of threads

Source: Internet
Author: User
At any point in time, threads can be combined (joinable) or separated (detached ). A combined thread can be reclaimed and killed by other threads. Its memory resources (such as stacks) are not released before being recycled by other threads. On the contrary, a separate thread cannot be recycled or killed by other threads, and its memory resources are automatically released by the system when it is terminated.

Separation and integration of threads

At any point in time, threads can be combined (joinable) or separated (detached ). A combined thread can be reclaimed and killed by other threads. Its memory resources (such as stacks) are not released before being recycled by other threads. On the contrary, a separate thread cannot be recycled or killed by other threads, and its memory resources are automatically released by the system when it is terminated.

The separation status of a thread determines how a thread terminates itself. In the above example, we use the threadIs not separated (can be combined, joinable, need to be recycled)In this case, the original thread waits for the end of the created thread. Only when the pthread_join () function returns, the created thread is terminated and the system resources occupied by it can be released. The separation thread is not like this. It is not waiting by other threads. When the running ends, the thread is terminated and system resources are released immediately. Programmers should select appropriate separation States based on their own needs.

The function for setting the thread separation status is pthread_attr_setdetachstate (pthread_attr_t * attr, int detachstate ). The second parameter can be PTHREAD_CREATE_DETACHED and PTHREAD _ CREATE_JOINABLE ). Note that if you set a thread as a separate thread and the thread runs very fast, it is likely to terminate before the pthread_create function returns, after it is terminated, it may hand over the thread number and system resources to other threads for use. In this way, the thread that calls pthread_create gets the wrong thread number. To avoid this situation, you can take some synchronization measures. One of the simplest methods is to call the pthread_cond_timewait function in the created thread, so that the thread can wait for a while, leave enough time for the function pthread_create to return. Setting a wait time is a common method in multi-threaded programming. However, do not use functions such as wait (), which sleep the entire process and cannot solve the thread synchronization problem.

Another common attribute is the thread priority, which is stored in the schema sched_param. Use the pthread_attr_getschedparam function and the pthread_attr_setschedparam function to store the data. Generally, we always take the priority and modify the obtained value before storing it back.

4) thread wait-correct processing of thread termination

# Include

Void pthread_exit (void * retval );

Void pthread_join (pthread_t th, void * thread_return); // wait until th ends, * thread_return = retval;

Int pthread_detach (pthread_t th );

If the thread is inJoinable status, onlyThe thread can only be created to wait for termination.

In Linux, although each thread is independent of each other, termination of one thread will not be notified or affect other threads. However, the resources of a terminated thread will not be released with the termination of the thread. We need to call pthread_join () to obtain the termination status of another thread and release the resources occupied by the thread. (Note: The thread is inUnder joinable status)

The thread that calls this function will be suspended, waiting for the end of the thread indicated by th. Thread_return is a pointer to the return value of the thread th. Note that the thread indicated by th must be joinable, that is, it is in a non-detached (free) state, and only one thread can call pthread_join () on th (). If th is in the detached state, an error is returned for the call to th's pthread_join.

If you do not care about the end state of a thread, you can set a thread to the detached state so that the operating system can recycle its resources at the end of the thread. You can set a thread to the detached status in two ways. One is to call the pthread_detach () function and set the thread th to the detached state. Another method is to set the thread to the detached state when it is created. First, initialize a thread attribute variable and set it to the detached state, finally, pass it as a parameter to the thread creation function pthread_create (), so that the created thread is directly in the detached state.

Create a detach thread:

Pthread_t tid;

Pthread_attr_t attr;

Pthread_attr_init (& attr );

Pthread_attr_setdetachstate (& attr, PTHREAD_CREATE_DETACHED );

Pthread_create (& tid, & attr, THREAD_FUNCTION, arg );

In short, in order to use pTo prevent the thread resources from being correctly released at the end of the thread, so as to avoid potential memory leakage. When the thread ends, ensure that the thread is in the detached state, otherwise, you need to call the pthread_join () function to recycle its resources.

Related Article

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.