Understand [Linux] thread separate state

At any one point in time, the thread is bound (the Joinable), or is separated (detached). A binding thread to be recovered and its resources to kill other threads; other threads before it is recycled, its memory resources (e.g., stack) is not released. Instead, a separate thread can not be recycled or kill another thread, which automatically releases the memory resources by the system when it terminates.

        Separating a thread state of the thread determine in what way to terminate itself. Thread a non-separated state of default, in which case, the original thread wait for the end of the thread is created. Only when pthread_join () function returns, the thread creation be considered terminated, in order to release system resources they consume. The separate thread is not like this, it is not waiting for the other thread, run their own end, the thread will cease immediately release system resources. Programmers should be according to their needs, select the appropriate separation of state. So if we know that when you create a thread to terminate without knowing the state of the thread, the thread can detachstate property pthread_attr_t structure, so that the thread start to separate state.

Function sets the state of the thread separating pthread_attr_setdetachstate (pthread_attr_t * attr, int detachstate ). The second parameter is optional PTHREAD_CREATE_DETACHED (separate threads) and PTHREAD _CREATE_JOINABLE (non-separated thread). To note here is that if you set a thread as a separate thread, and this thread runs and very fast, it is likely that before the pthread_create function returns would cease, it is possible to transfer the thread number and system resources to other after its termination threads, so call pthread_create thread get the wrong thread number. To avoid this synchronization can take certain measures, one of the easiest way is to call a function in pthread_cond_timewait thread is created, let this thread to wait for a while, leaving enough time for the function pthread_create return. Setting a waiting time is commonly used in multi-threaded programming in the method. But be careful not to use functions such as wait () and the like, which is to make the whole process of sleep, does not solve the problem of thread synchronization.

Another possible property is common priority thread, which is stored in the structure sched_param. And functions performed by the function pthread_attr_getschedparam pthread_attr_setschedparam storage, in general, we always take the first priority, to modify the value obtained after the deposit back.

Thread waiting for - the right to terminate processing thread

#include

void pthread_exit(void *retval);

void pthread_join (pthread_t th, void * thread_return); // wait for the end hangs th, * thread_return = retval;

int pthread_detach(pthread_t th);

If the thread is joinable state, it can only can only be created thread waits for his termination.

In the Linux platform by default, although between the various threads are independent of each other, a thread termination notice or will not go to affect other threads. But the resource thread has been terminated and will not end with the termination of threads and released, we need to call pthread_join () to get the state to terminate another thread and the thread release resources occupied. (Note: the thread is in joinable state)

The function is called thread hangs waiting for the end th expressed thread. thread_return thread pointer is pointing to th return value. Note that the thread th must be joinable represented, that is in a non-detached (free) state; and can only have only one thread calls pthread_join of th (). If th in the detached state, then the th of pthread_join () call will return an error.

If the state does not care about the end of a thread, then the thread can also be set to a detached state, so that the operating system at the end of the thread to recover its share of the resources. A thread set detached state can be achieved in two ways. One is calling pthread_detach () function, you can set the thread th detached state. Another method is to thread is created it will be set to the detached state, first initializes a variable thread attribute, then set it to detached state, and finally it will create a function pthread_create () as a parameter thread, so that the created out thread directly in a detached state.

Creating detach threads:

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);

总之为了在使用 pthread 时避免线程的资源在线程结束时不能得到正确释放,从而避免产生潜在的内存泄漏问题,在对待线程结束时,要确保该线程处于 detached 状态,否着就需要调用 pthread_join() 函数来对其进行资源回收。

http://www.cnblogs.com/mydomain/archive/2011/08/14/2138454.htm

Guess you like

Origin www.cnblogs.com/yiyide266/p/11227564.html