Separation State of threads

Source: Internet
Author: User
Tags terminates

At any point in time, the thread is either associative (joinable), or detached (detached). A binding thread can be recovered by another thread and killed, and its memory resource (such as a stack) is not freed until it is reclaimed by another thread. Instead, a disconnected thread cannot be recycled or killed by other threads, and its memory resources are automatically freed by the system when it terminates.

The separation state of a thread determines how a thread terminates itself. In the default case, the thread is non-detached, and in this case, the original thread waits for the creation of the threads to end. Only when the Pthread_join () function returns does the thread created terminate to free the system resources that it occupies. The separation thread is not the case, it is not waiting for the other threads, the end of their own run, the thread is terminated, immediately release the system resources. Programmers should choose the appropriate separation state according to their own needs. So if we know that we don't need to know the terminating state of the thread when we create the thread, we can pthread_attr_t the Detachstate thread property in the struct and let the thread start in a detached state.

The function to set the thread detach state is pthread_attr_setdetachstate (pthread_attr_t *attr, int detachstate). The second parameter can be selected as pthread_create_detached (Detach thread) and PTHREAD _create_joinable (non-detached thread). One thing to note here is that if a thread is set to detach a thread, and the thread runs very fast, it is likely to terminate before the Pthread_create function returns, and it may terminate the thread number and system resources for use by other threads, so that the call Pthread_ The thread of create gets the wrong thread number. To avoid this situation, one of the simplest ways to do this is to call the Pthread_cond_timewait function in the created thread to allow the thread to wait for a while, leaving enough time for the function pthread_create to return. Setting a wait time is a common method in multithreaded programming. But be careful not to use functions such as wait (), which make the whole process sleep, and do not resolve thread synchronization issues.

Another potentially common attribute is the priority of the thread, which is stored in the structure Sched_param. Using function Pthread_attr_getschedparam and function Pthread_attr_setschedparam to store, generally speaking, we always first take priority, the value of the obtained modified and then stored back.

Thread waits--correctly handles thread termination

#include <pthread.h>

void Pthread_exit (void *retval);

void Pthread_join (pthread_t th,void *thread_return);//hang wait for th end, *thread_return=retval;

int Pthread_detach (pthread_t th);

If the thread is in the joinable state, it can only be created by his thread waiting to be terminated.

On the Linux platform by default, the termination of one thread does not notify or affect other threads, although the threads are independent of each other. But the resources of the terminated thread will not be freed as the thread terminates, we need to call Pthread_join () to get the terminating state of the other thread and release the resources that the thread occupies. (Note: The thread is in joinable state)

The thread that called the function hangs, waiting for the end of the thread represented by th. Thread_return is a pointer to the thread th return value. It is important to note that the thread represented by th must be joinable, that is, in a non-detached (free) state, and only one thread can have a unique call to th pthread_join (). If th is in the detached state, then the Pthread_join () call to TH will return an error.

If you do not care about the end state of a thread, you can also set a thread to the detached state, allowing the operating system to reclaim the resources it occupies at the end of the thread. Setting a thread to the detached state can be done in two ways. One is to call the Pthread_detach () function to set thread th to detached state. Another approach is to set it to the detached state when the thread is created, initialize a thread property variable first, then set it to the detached state, and finally pass it as a parameter to the thread creation function pthread_create () so that the created thread is directly in the Detached status.

To 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 summary, in order to avoid a potential memory leak problem when the thread's resources are not properly freed when using pthread, to ensure that the thread is in the detached state at the end of the thread, no need to call Pthread_join () function to recycle the resource.

Separation State of threads

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: 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.