Threading Concepts
A thread, sometimes called a lightweight process (lightweight PROCESS,LWP), is the smallest unit of program execution flow.
A thread is a single sequential control flow in a program. A relatively independent, scheduled execution unit within a process is the unit of dispatch for the system to dispatch and dispatch CPUs independently of the operating program. Running multiple threads at the same time in a single program accomplishes different tasks, called multithreading.
Thread Resources
Because multiple threads in a program share the same address space, the data segment content is shared. In addition, the following are also shared:
1. File Descriptor Descriptor
2. How each signal is processed (sig_ign, SIG_DFL, or custom signal processing functions)
3. Current working directory
4. User ID and Group ID
But some of the resources are each thread has one copy:
1. Thread ID
2. Context, including values for various registers, program counters, and stack pointers
3. Stack space
4. errno variables
5. Signal Shielding Word
6. Scheduling priority
Line Program Control system
Created: int pthread_create (pthread_t *thread, const pthread_attr_t *attr,void * (*start_routine) (void *), void *arg);
Gets the thread id:pthread_t pthread_self (void);
Terminating a thread
If you need to terminate only one thread without terminating the entire process, there are three ways to do this:
1. Return from the thread function. This method does not apply to the main thread, and 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. Threads can call Pthread_exit to terminate themselves. Terminate yourself
Waiting for thread
int Pthread_join (pthread_t thread, void **retval);
The thread that is called to wait for the function suspends the wait until the thread with the ID thread terminates. Threads are terminated in different ways, and the results of Pthread_join are different. Specific as follows:
1 The thread terminates in return, so the cell that retval points to holds the return value of the thread function.
2 If the process is terminated by the Pthread_cancel method, then retval holds the constant pthread_canceled
3 If the process terminates itself Pthread_exit way, then Reval save the parameters that the user passed to exit
Note: The state of the thread will be the detach state (detach) after the thread is join, and the same pthread_cancel function can detach the thread. Therefore, you cannot join and detach a single thread at the same time
Thread separation
At any point in time, the thread is either associative (joinable) or separable (detached). A binding thread is a resource that can be recovered and killed by other threads. Before being recycled, his memory resources (stacks, etc.) are not released. For a thread with a detached state, its resources cannot be withdrawn and killed by another thread until the thread ends to be automatically freed by the system
By default, the thread state is set to combined. So to avoid problems such as resource leaks, a thread should be a join or detach that is displayed, otherwise the thread's state is similar to the zombie process in progress. There will be some resources that are not recycled.
Call function Pthread_join, the main thread will be blocked when the wait thread is not terminated. If you want to avoid blocking, then
Add code Pthread_detach (THREAD_ID) to the main thread
or join Pthread_detach (thread_self ()) in the waiting thread
Linux thread control & thread separation