2. Linux multithreading and 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) it is 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 threads determines how a thread terminates itself . In the preceding example, we use the default attributes of the thread , 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 thread to be created. The created thread is terminated only when the pthread_join () function returns, 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.

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

Another common attribute is the thread priority, which is stored in the schema sched_param. Use the pthread_attr_getschedparam function and the pthread_attr_setschedparam function to store the data. Generally, 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); // wait until th ends, * thread_return = retval;

Int pthread_detach (pthread_t th );

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

by default, although each thread is independent of each other, the termination of one thread will not notify or affect other threads. However, the resources of a terminated thread will not be released with the termination of the thread. We need to call pthread_join () to obtain the termination status of another thread and release the resources occupied by the thread. (Note: the thread is in joinable Status )

the thread that calls this function is suspended, wait for the end of the thread indicated by Th. Thread_return is a pointer to the return value of the thread th. Note that the thread indicated by th must be joinable, that is, it is in a non-detached (free) state, and only one thread can call pthread_join () on th (). If TH is in the detached state, an error is returned for the call to th's pthread_join.

If you do not care about the end state of a thread, you can set a thread to the detached state so that the operating system can recycle its resources at the end of the thread.You can set a thread to the detached status in two ways. One is to call the pthread_detach () function and set the thread th to the detached state. Another method is to set the thread to the detached state when it is created. First, initialize a thread attribute variable and set it to the detached state,Finally, pass it as a parameter to the thread creation function pthread_create (), so that the created thread is directly in the detached state.

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

to use P when the thread is finished, the thread resources cannot be correctly released at the end of the thread, thus avoiding potential memory leakage. When the thread ends, to ensure that the thread is in the detached state, you need to call pthread_join () Function.

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.