Python's concurrent programming (iv) multi-threaded (theory)

Concurrent programming as much as a thread (theory)

  1. What is the thread

    Thread: As the name suggests, is the process of assembly line work, a pipeline must belong to a workshop, a workshop of the working process is a process

    Therefore, the process is only used to focus 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 that multiple threads of control within a process, a plurality of threads to share the control of the process address space, corresponding to a plurality of lines within a plant, a plant are shared resource . (A process which open multiple threads (a process which share the same memory space))

    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.

    note:

    1. Therefore, the process of which the real work is thread (process inside thread)

    2. The process just used to isolate resources with each other, and the thread is really responsible to mobilize his cpu

  2. The cost of creating a small thread

    Create a process overhead is much larger than the thread?

    If our software is a factory that a number of lines, assembly line work requires power, the power that is only a cpu (single core cpu)

    A workshop is a process, a workshop at least one assembly line (a process at least one thread)

    Create a process, is to create a workshop (application space, at least in this space built an assembly line)

    Built thread, just build a pipeline in a workshop, no need to apply for space, so to create a small overhead

  3. The difference between threads and processes

    1. Create a small thread than the cost of the process (a process open, which will have the space, and the thread inside the process, there is no need to open a space in a)
    2. Multi-threaded must be open on the inside of a process, the process of sharing inside resources
    3. fast start thread
    shared by multiple threads 4. the process under the same process resources and memory space among the plurality of processes is isolated from
    n-= 100
    DEF Work ():
    Global n-
    N- = 100
    if the opening n process is independent of each other, and the thread is a shared resource, not isolated

    In the process of opening wins, the child will not copy the parent process of
    open processes under linux, the child will be a full copy of the parent process

    5. thread communication within the thread can simply asked where the process of

  4. Why do you use multithreading

    Multithreading refers to open multiple threads within a process, simply say: If multiple tasks share an address space, you must open multiple threads within a process. Details speaking divided into four points:

    1. Multithreading share a process address space

    2. Thread the process more than the lightweight threads easier to create than the process can be revoked, in many operating systems, create a thread than to create a process to be 10-100 times faster, in a large number of threads need to modify the dynamic and fast this feature is useful

    3. If multiple threads are cpu intensive, and then the reinforcing performance can not be obtained, but if a large amount of computation and a large number of I / O processing is present, has a plurality of threads run allows these activities overlap with each other, which will speed up program execution.

    4. In a multi-cpu systems, in order to maximize the use of multi-core, you can open multiple threads, much smaller than the open overhead to the process. (This does not apply to a python)

Guess you like

Origin www.cnblogs.com/zhangdadayou/p/11431932.html