I think the thread may be something that loves and hates. Love it is: In many cases it can improve the structure of the program, save system resources, and even improve the efficiency of the system to a certain extent. What hates it is: it is easy to make mistakes, about this, I realized the deepest, our system is due to a serious problem due to threads. But the words are coming back, we can't scramble, often say "artistic people," I think as long as we have enough knowledge and experience, threads will become a tool in our hands.
I was developed on Solaris. Here I would like to introduce two thread models on Solaris, which describes the basic knowledge of thread development on the platform.
There are two thread libraries on Solaris, one is a PSIX standard PThread library, one is Sun's own Thread library, here I mainly describe the POSIX standard. When you use the POSIX function to set the thread attribute, you often encounter attribute pthread_scope_process, then use the thread created with this property called the unbound thread, and the thread created by the attribute pthread_scope_system is called Bound thread, then unbound and bound threads have anything Difference? Below I will describe the two thread models on Solaris and give the answer from it.
TWO-level: standard implementation model on Solaris
The LWP in this figure is a sense of lightweight process. Here you can think it is the CPU. Once the user space thread has obtained LWP, that is, it obtains executive power, it can be scheduled at any time.
We look at the left half of this figure, we see the THREAD and kernel space of the user space is not a one-on-one relationship, but the relationship between M., then get the LWP by that thread, this scheduling is completed by The thread library is completed, not the participation of the kernel, that is, the thread context of this approach is more fast, and it is clear that this way is more bucket, and Solaris provides a LWP pool according to certain strategies for more Thread sharing, this thread is the unbound thread.
Look at the right half of this picture, we can see the thread and lwp of the user space Thread and LWP are one-to-one correspondence, each thread has a permanent LWP for it, which is obvious that threads in this way. Real-time scheduling is good, but the context switch of this thread requires the participation of Kernel, and because the LWP itself is consumed operating system resources, this way is wasting resources than the above way, this approach is called so-called Bound thread. Optional implementation model on one-level: Solaris
This model is actually providing consistent threads and LWP's one-to-one correspondence, without Thread and LWP N to M relationship, it is clear that the One-Level model should be more system resources, but may increase the program. effectiveness.
For programs to recompile, you can add compilation option -R / usr / lib / lwp to use the One-Level model, for the compiled program, then add / usr / lib / lwp in the LD_Library_Path environment variable setting .