1 Introduction
Thread technology was introduced early in the 60, but the real application of multithreading to the operating system was in the middle of the 80, and Solaris was the leader in this area. Traditional UNIX also supports the concept of threading, but only one thread is allowed in a process (processes), so multiple threads mean multiple processes. Now, multithreading technology has been supported by many operating systems, including WINDOWS/NT, and of course, Linux.
Why do we have to introduce threads when we have the concept of a process? What are the benefits of using multithreading? What system should choose multithreading? We must first answer these questions.
One reason to use multithreading is that it is a very "frugal" multitasking operation compared to a process. We know that under a Linux system, starting a new process must be assigned to its own address space, a large number of data tables to maintain its code snippets, stack segments and data segments, which is an "expensive" multitasking way of working. While running on multiple threads in a process, they use the same address space, share most of the data, and start a thread much less space than it takes to start a process, and the time it takes to switch between threads is much less than the time it takes to switch between processes. According to statistics, overall, the cost of a process is about 30 times times the cost of a thread, of course, on the specific system, this data may be a big difference.
The second reason to use multithreading is the convenient communication mechanism between threads. For different processes, they have a separate data space, the transmission of data can only be carried out by means of communication, which is not only time-consuming, but also inconvenient. Threads Otherwise, because the data space is shared between threads under the same process, the data for one thread can be used directly for other threads, which is quick and convenient. Of course, the sharing of data also brings some other problems, some variables can not be modified by two threads at the same time, some subroutines declared as static data is more likely to bring a catastrophic attack on multithreaded programs, which is the most need to note when writing multithreaded program.
In addition to the above mentioned advantages, not compared with the process, multithreaded procedure as a multitasking, concurrent work, of course, has the following advantages:
1 Improve application response. This is especially useful for graphical interface programs, when an operation takes a long time, the whole system will wait for this operation, when the program does not respond to keyboard, mouse, menu operation, and using multithreading technology, the time-consuming operation (time consuming) on a new thread, you can avoid this embarrassing situation.
2 make the multiple CPU system more efficient. The operating system guarantees that when the number of threads is not greater than the number of CPUs, different threads run on different CPUs.
3) Improve the structure of the program. A long and complex process can be considered into multiple threads to become several independent or half independent parts of the operation, such programs will facilitate understanding and modification.
Now let's try to write a simple multithreaded program.