Linux multi-threading, threading separation and integration

Source: Internet
Author: User

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

Separation and integration of threads

at any point in time, the thread is 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.

set the thread detach state function to 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 you set a thread to detach the thread, and the thread runs very fast, it is likely that the pthread_create pthread_create #include <pthread.h> in the thread being created;

void Pthread_exit (void *retval);

th end , *thread_return=retval;

int Pthread_detach (pthread_t th);

if the thread is in joinable State, you can only 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 thethThe end of the represented thread. Thread_returnis point to threadtha pointer to the return value. It is important to note thatthThe thread that is represented must bejoinable, i.e. in a non-detached(free) state, and only one thread can have a singlethcalledPthread_join (). Ifthindetachedstate, then thethof thePthread_join ()the call 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. 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 using Pthread to prevent thread's resources from being properly freed at the end of threads, make sure that the thread is at the end of the process Detached State, no need to call the pthread_join () function to recycle it.  

(RPM) Linux multithreading, threading separation and integration

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.