Details of pthread_join and pthread_detach

Source: Internet
Author: User

Pthread_t pthr;

Pthread_create (& pthr, null, thread_handler, null );

...

Void * thread_handler (void * Arg)

{

/* Do something */

Pthread_join (pthr, null );

}

 

The above code is not good. pthread_join cannot be placed in the handler called by pthread. Although no error is reported, the thread cannot be recycled normally. If you create a thread multiple times, memory will increase (another form of Memory leakage ).

The correct method is pthread_join outside handler:

 

Pthread_t pthr;

Pthread_create (& pthr, null, thread_handler, null );

Pthread_join (pthr, null );

...

Void * thread_handler (void * Arg)

{

/* Do something */

}

 

If pthread_join is not used, what about pthread_detach? That is the most convenient, but note: it is best to put pthread_detach In the first sentence of handler.

 

Void * thread_handler (void * Arg)

{

Pthread_detach (pthr );

/* Do something */

}

 

If pthread_create is followed by pthread_detach, an error may occur.

 

Pthread_detach (pthread_self ())

Linux thread execution is different from windows. pthread has two states: joinable and unjoinable,

If the thread is in the joinable state, the stack and thread descriptor occupied by the thread will not be released when the thread function returns exit or pthread_exit (a total of more than 8 K ). These resources will be released only after you call pthread_join.

For unjoinable threads, these resources are automatically released when the thread function exits or pthread_exit. The unjoinable attribute can be specified at pthread_create or pthread_detach itself in the thread after the thread is created, for example, pthread_detach (pthread_self (). Change the status to unjoinable to ensure resource release. Or set the thread to joinable and call pthread_join in due time. in fact, simply put, if the thread function header is added with pthread_detach (pthread_self (), the thread state changes and the pthread_exit thread directly exits at the end of the function. Eliminating the trouble of wiping a thread's ass pthread_self

Header file

# Include <pthread. h>

Function prototype

Pthread_t pthread_self (void); function: Obtain the ID of the thread. The pthread_t type is unsigned long int, so % lu is used for printing. Otherwise, a strange result is generated.

Function

Obtains the thread identifier (ID) of the current calling thread ). recently we found that the return value of calling pthread_detach (pthread_self () in the first line of the thread function is 22 rather than 0. Then we found the following words on the Internet: Linux thread execution is different from windows, pthread has two states: joinable and unjoinable. If the thread is joinable, when the thread function returns exit or pthread_exit, the stack and thread descriptor occupied by the thread are not released (more than 8 K in total ). These resources will be released only after you call pthread_join.

For unjoinable threads, these resources are automatically released when the thread function exits or pthread_exit. The unjoinable attribute can be specified at pthread_create or pthread_detach itself in the thread after the thread is created, for example, pthread_detach (pthread_self (). Change the status to unjoinable to ensure resource release. Or set the thread to joinable and call pthread_join in due time. check the/proc/<pid>/maps file while running the program. If you see a lot of Virtual Memory fragments about 8 KB, it can be basically confirmed that pthread_create fails after 300 threads due to thread resource leakage. I wonder if the following attributes are set for the thread to be created because of myself,

Pthread_attr_init (& ATTR );

Pthread_attr_setdetachstate (& ATTR, pthread_create_detached );

Pthread_detach (pthread_self (); the two sections of code conflict with each other. ========================================================== ============================================================ Pthread_detach (threadid) the difference between pthread_detach (pthread_self () and pthread_detach is that their threads are called differently, and there is no other difference. The function of the pthread_detach (threadid) function is to make the thread whose thread ID is threadid in the separation state. Once the thread is in the separation state, the underlying resources are immediately recycled when the thread is terminated; otherwise, the sub-thread state will be permanently saved (occupying system resources) until the main thread calls pthread_join (threadid, null) to obtain the exit state of the thread.

Generally, after the main thread uses pthread_create () to create a subthread, it can call pthread_detach (threadid) to separate the created subthread. threadid here refers to the threadid of the subthread, when this sub-thread stops, underlying resources are immediately recycled;

The created sub-thread can also separate itself. The sub-thread calls pthread_detach (pthread_self () to separate itself, because the pthread_self () function returns its own thread ID.

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.