2. Linux multithreading, thread separation and integration)

Source: Internet
Author: User

(2) 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.;Before being recycled by other threads, its memory resources (such as stacks) are not released. 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 preceding example, we use the thread default attribute, that is, it is a non-separated state (can be combined, joinable , need to be recycled) in this case, the original thread waits for the end of the created thread. Only when P Thread_join when the () function returns, the created thread is terminated, in order to release the system resources occupied by you. 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. Program personnel should select the appropriate separation status based on their own needs.

set the function for thread separation to pthread_attr_setdetachstate ( pthread_attr_t * ATTR, int detachstate ). The second parameter can be pthread_create_detached (separating threads) and pthread _ create_joinable (non-detached thread ). Note that if you set a thread as a separate thread and the thread runs very fast, it is likely to be in pthread_create the function is terminated before it returns, after it is terminated, it may hand over the thread number and system resources to other threads for use, so that the call pthread_create the thread gets the wrong thread number. To avoid this situation, you can take some synchronization measures. One of the simplest methods is that you can call it in the created thread Pthread_cond_timewait Function, Let this thread wait for a while, set enough time for the functionPthread_create. Setting a wait time is a common method in multi-threaded programming. But do not useWait() And other functions, which sleep the entire process and cannot solve the thread synchronization problem.

Another common attribute is the thread priority, which is stored in the StructureSched_param. FunctionsPthread_attr_getschedparamAnd functionsPthread_attr_setschedparamIn general, we always take the priority and modify the obtained value before storing it back.

4) thread wait-correct processing of thread termination

# Include <pthread. h>

Void pthread_exit (void * retval );

Void pthread_join (pthread_t th, void * thread_return); // pendingThEnd, * Thread_return = retval;

Int pthread_detach (pthread_t th );

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

InLinuxBy default, although each thread is independent of each other, the termination of one thread will not be notified or affect other threads. However, the resources of the terminated thread will not be released with the termination of the thread. We need to callPthread_join ()To obtain the termination status of another thread and release the resources occupied by the thread. (Note:The thread is inJoinableStatus)

The thread that calls this function will be suspended, waiting Th End of the thread. Thread_return Yes to thread Th Pointer of the returned value. Note that Th The thread must be Joinable Is not Detached (Free) status, and only one thread can have Th Call Pthread_join () . If Th In Detached Status Th Of Pthread_join () The call will return an error.

if you do not care about the end status of a thread, you can also set a thread to detached Status, which allows the operating system to reclaim resources occupied by the thread at the end of the thread. set a thread to detached the status can be achieved in two ways. One is to call pthread_detach () function, you can set the thread th set to detached status. Another method is to set it to detached Status: initialize a thread attribute variable, set it to detached Status, Finally, pass it as a parameter to the thread to create a function.Pthread_create ()In this way, the created thread is directly inDetachedStatus.

CreateDetachThread:

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

to use P when a thread ends, it is prevented that the thread's resources cannot be correctly released at the end of the thread, to avoid potential memory leakage, make sure that the thread is in the detach Ed Status, which must be called if not pthread_join () function to recycle resources.

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.