Linux thread control & amp; thread separation, linux thread

Source: Internet
Author: User
Tags terminates

Linux thread control & Thread separation, linux thread
Thread Concept

A thread, sometimes called a Lightweight Process (LWP), is the smallest unit of the program execution flow.

A thread is a single sequential control flow in a program. A relatively independent and schedulable execution unit in a process is the basic unit for Independent System Scheduling and CPU allocation, which refers to the scheduling unit of the running program. It is called multithreading.

Thread Resources

Because multiple threads in a program share the same address space, the content of the Code segment and data segment is shared. In addition, the following content is shared:

1. file descriptor table
2. Processing Method of each signal (SIG_IGN, SIG_DFL, or custom signal processing function)
3. current working directory
4. User id and group id

However, some resources have a copy of each thread:

1. Thread id

2. context, including the values of various registers, program counters, and stack pointers
3. Stack space
4. errno variable
5. signal shielding characters
6. Scheduling Priority

 

Thread control

Create: int pthread_create (pthread_t * thread, const pthread_attr_t * attr, void * (* start_routine) (void *), void * arg );
Obtain the thread id: pthread_t pthread_self (void );

Terminate thread

If you only need to terminate a thread without terminating the entire process, there are three methods:
1. return from the thread function. This method is not applicable to the main thread. return from the main function is equivalent to calling exit. // Thread return
2. One thread can call pthread_cancel to terminate another thread in the same process. // Passive termination
3. The thread can call pthread_exit to terminate itself. // Manually terminate

Waiting thread

Int pthread_join (pthread_t thread, void ** retval );
The thread that calls this function will wait until the thread whose id is thread terminates. When a thread is terminated in different ways, pthread_join produces different results. The details are as follows:

1. When a thread terminates with return, the Unit indicated by retval stores the return value of the thread function.

2. if the process is terminated by the pthread_cancel method, retval stores the constant PTHREAD_CANCELED.

3. If the process terminates itself in the pthread_exit mode, reval stores the parameters passed to exit by the user.

Note: After a thread is joined, the thread state is detach. The same pthread_cancel function can be used to separate the thread. Therefore, join and detach operations cannot be performed on a thread at the same time.

Thread Separation

At any point in time, threads can be combined (joinable) or detached (detached ). A combiner thread can be used by other threads to reclaim and kill resources. Before being recycled, his memory resources (stacks, etc.) are not released. For a thread in the detached state, its resources cannot be reclaimed or killed by other threads. It can be automatically released by the system only when the thread ends.

By default, the thread status is set to combined. To avoid problems such as resource leakage, a thread should be displayed as a join or detach. Otherwise, the thread state is similar to Zombie Process in the Process. Some resources are not recycled.

Call the pthread_join function. When the waiting thread is not terminated, the main thread is blocked. To avoid blocking

Add the code pthread_detach (thread_id) to the main thread)

Or add pthread_detach (thread_self () to the waiting thread ())

Related Article

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.