2-1 Thread theory

What is a thread
in traditional operating systems, each process has an address space, but by default there is a thread of control

Thread name suggests, is an assembly line process work (assembly line work needs power supply, power is equivalent cpu), and a pipeline must belong to a workshop, a workshop of the working process is a process, workshop leader to integrate resources together, is a resource units, and have at least one line within a workshop.

So, just the process used to concentrate resources together (process just a resource unit, or set of resources), and on the implementation of the unit thread is the cpu.

Multithreading (i.e. multiple threads of control) concept is the presence of multiple threads in a process, a plurality of threads to share the address space of the process, corresponding to a plurality of lines are a shared resource within a plant plant. For example, Beijing Subway and Shanghai Metro is a different process, and Beijing subway Line 13 is a thread, all Beijing subway lines share all resources Beijing subway, for example, all passengers can be pulled all the lines.

二 线程与进程的区别
Threads share the address space of the process that created it; processes have their own address space.
Threads have direct access to the data segment of its process; processes have their own copy of the data segment of the parent process.
Threads can directly communicate with other threads of its process; processes must use interprocess communication to communicate with sibling processes.
New threads are easily created; new processes require duplication of the parent process.
Threads can exercise considerable control over threads of the same process; processes can only exercise control over child processes.
Changes to the main thread (cancellation, priority change, etc.) may affect the behavior of the other threads of the process; changes to the parent process does not affect child processes.
To summarize the above difference, nothing but two key points, which is why we It requires the use of multiple threads of reason in a particular scene:

Multiple threads within the same process shared address resources in the process of
creating a thread overhead is much less than the cost of the creating process (a process is created, is to create a workshop, involving the application space, but the space is built-in at least one line but to create a thread, just build a pipeline in a workshop, no need to apply for space, so to create a small cost)
more than three-threaded applications, for example
to open a word processing software process that certainly need to do more than one thing, such as monitor keyboard input , word processing, timed text automatically saves to the hard disk, the operation of these three tasks are the same piece of data, and therefore can not use multiple processes. Only three threads concurrently open in a process where, if it is single-threaded, it is only when the keyboard input, can not handle text and automatically saved, and can not be entered automatically save the time and word processing.

Multi-threaded application examples

Guess you like

Origin www.cnblogs.com/shibojie/p/11664768.html