Linux multithreaded programming and Linux 2.6 under the NPTL

Source: Internet
Author: User
Tags mutex posix

These days due to the work needs, pondering a bit of Linux under the multithreading of the relevant information. The most common multithreaded support library under Linux is the Pthread library, which is an integral part of the GLIBC library. However, there is a paucity of documentation on Pthread, especially the introduction of POSIX multithreaded specifications and the introduction of multi-threaded implementations in pthread libraries is rare. Multithreaded programming is a technology that must be mastered by system programmers, so it always makes learning programmers feel headache. I also do not have too many multithreaded programming experience, here just to collect some of my own on the Linux multithreading also calculate the new data to make a summary, in order to learn from each other exchange.

Here, incidentally, there is a multithreaded book, Posix multithreaded Programming, which is the English version of the programming with POSIX muiltthread, which is the only topic I can find in the past six months to introduce multithreaded programming. I personally feel that the contents of one of the first 1/3 of the book are good, but the things that follow are very obscure and have a lot of obvious textual errors. The translator of this book is a few people, it is estimated that each person's translation ability is different to cause the book's half-baked. So I don't recommend that you buy this book as a Bible collection. This book is the first half of the content of the main focus on POSIX multithreading, introduced a more wonderful is a number of multithreaded programming model, the multithread mutex and synchronization mechanism of the very good, recommended a look. These are not the original book, early in the "UNIX Network programming," the second volume of interprocess communication has these classic introduction, but can systematically combine these mechanisms into multithreaded programming to have a remarkable place. In addition, "UNIX Network Programming" two volumes of content is too old, the book is too thick, not most programmers can sit down and look at. Here I would also like to say a few words about Microsoft's lack of technical reprimand. The content of Windows multithreaded programming in the Platform SDK section in MSDN is just plain ridiculous, only silly to create and exit the thread of the function, about mutual exclusion, the introduction of the conditions are all nothing. Only in its sample code to find their own, sample code inside the thread synchronization method incredibly is to do a dead loop to Deng, also do not know it to sell windows so much money is what to eat. The multi-threading package in MFC looks like a thing, but I can't imagine how Microsoft actually implemented MFC's online functionality on such a rudimentary system API. Windows advocates do not throw eggs here, it is best to write a multithreaded introduction to Windows besides. It's more meaningful than breaking an egg. Well, the book goes back to the story. Continue to say multithreading on Linux.

On Linux, from the kernel point of view, there is basically no difference between threads and processes-everyone is a process. Multiple threads of a process are just a few special processes they share the same code context, although they have their own process description structure. On Linux, such processes are called lightweight processes light weight process. This is about the overall concept of threading, and we tend to start our multithreaded programming journey with this concept in our grasp. This is enough to get started with multithreaded programming, but the fact is that threads are much more complex. First, the priority scheduling between multithreading, memory resource (stack) allocation and signal delivery is not simple to share the same process code context can be resolved. Secondly, the problem of efficiency: How to use multiple CPU resources effectively (2.4 kernel multithreading can not use multiple CPUs, a process thread is limited to run on the same CPU). Therefore, the implementation of multi-line threading Pthread is not a simple matter, it is based on the unique threading model.

In the Linux 2.4 kernel, a kernel thread is used in the Linux kernel to handle the context switching (thread switching) of multiple threads in a user-state process. Since there is no thread group concept in the kernel, that is, multiple threads of a process, you must rely on implementing an additional thread in the Pthread library to manage the creation, exit, resource allocation and recycling, and thread switching of other user threads (that is, user program-generated threads). Because the hardware does not have a thread register, such as winter to support multithreading, so the thread's switching performance and low, and need to introduce a complex mechanism in the process of the stack for each thread to separate the location of the stack data, and in the switch stack data copy. The biggest problem is the lack of support for the synchronization mechanism between threads in the kernel, so the Pthread library has to rely on signal mode to implement synchronization at the bottom, so the mutex operation and the conditional operation are converted to the signal operation of the process. The implementation of Pthread is full of extremely complex signal operations. We all know that the signal itself is a low-speed way of communication, so it is bound to slow down the actual performance of the thread. The final problem is signal processing, and because of the kernel's ignorance of threads, the management thread must receive the signal and post it to the corresponding thread, on the one hand, inefficient, on the other hand due to the uncertainty of the signal (such as reading a file suddenly error), to accurately deliver all the signals to the correct thread is difficult to guarantee.

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.