The multi-Threading theory part of Python concurrent programming

Source: Internet
Author: User
Tags posix processing text

Read Catalogue

    • What is a thread
    • Low-cost creation of second-tier processes
    • The difference between a third thread and a process
    • Four reasons to use multithreading
    • Five examples of multi-threading applications
    • Six classic threading models (Learn)
    • Seven POSIX threads (learn)
    • Eight threads implemented in user space (Learn)
    • Nine threads implemented in kernel space (learn)
    • 10 user-level vs. kernel-level threading (Learn)
    • 11 Hybrid implementations (Learn)
What is a thread

In traditional operating systems, each process has an address space, and there is a control thread by default

The thread as the name implies, is a pipeline work process, a pipeline must belong to a workshop, a workshop work process is a process

The workshop is responsible for the integration of resources together, is a resource unit, and a workshop has at least one pipeline

The line of work requires power, the power is equivalent to the CPU

Therefore, a process is simply used to centralize resources together (a process is just a resource unit, or a resource collection), and the thread is the executing unit on the CPU.

The concept of multi-threaded (that is, multiple control threads) is that there are multiple control threads in a process, and multiple control threads share the process's address space, which is equivalent to a workshop with multiple pipelines, sharing a workshop resource.

For example, the Beijing Metro and the Shanghai Metro are different processes, while the Beijing metro line Line 13 is a thread, the Beijing Metro all the lines share all the resources of the Beijing Metro, such as all passengers can be pulled by all lines.

Low-cost creation of second-tier processes

Is the cost of creating a process much larger than a thread?

If our software is a factory, the factory has a number of lines, the pipeline work requires power supply, only one CPU (single core CPU)

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

Creating a process is creating a workshop (application space, building at least one pipeline within that space)

and build a thread, just in a workshop built a pipeline, no need to apply for space, so the creation of a small cost

Between processes is a competitive relationship, between threads is a collaborative relationship?

The workshop is directly competitive/grab the power of the relationship, competition (different processes are directly competitive relationships, are different programmers write programs run, Thunderbolt preempt other processes of speed, 360 of other processes as the virus to dry dead)
A workshop of different assembly line collaborative work relationship (the same process of the thread is a partnership, is the same program written within the program to start, the thread within the Thunderbolt is a cooperative relationship, will not do their own)

The difference between a third thread and a process
    1. Threads share the address space of the process that created it; Processes has their own address space.
    2. Threads has direct access to the data segment of its process; Processes has their own copy of the data segment of the parent process.
    3. Threads can directly communicate with other Threads of its process; Processes must use interprocess communication to communicate with sibling processes.
    4. New threads is easily created; New processes require duplication of the parent process.
    5. Threads can exercise considerable control over Threads of the same process; Processes can only exercise control over the child processes.
    6. Changes to the main thread (cancellation, priority change, etc.) may affect the behavior of the other threads of the Proce ss Changes to the parent process does isn't affect child processes.
Four reasons to use multithreading

Multithreading refers to the opening of multiple threads in a process, simply speaking: If multiple tasks share a single address space, then multiple threads must be opened within a process. The detailed lecture is divided into 4 points:

1. Multithreading share the address space of a process

2. Threads are more lightweight than processes, threads are easier to create revocable than processes, and in many operating systems, creating a line turndown creates a process 10-100 times faster, which is useful when a large number of threads require dynamic and rapid modification

3. If multiple threads are CPU-intensive, there is no performance gain, but if there is a lot of computation and a lot of I/O processing, having multiple threads allows these activities to overlap each other, which speeds up the execution of the program.

4. In a multi-CPU system, in order to maximize the use of multicore, you can open multiple threads, much less than the open process overhead. (This one does not apply to Python)

Five examples of multi-threading applications

Open a word processing software process, the process must do more than one thing, such as listening to keyboard input, processing text, automatically save the text to the hard disk, the three tasks are the same piece of data, and therefore can not be used multi-process. Only in one process can open three threads concurrently, if it is a single thread, it can only be, keyboard input, can not handle text and auto-save, automatically save the text can not be entered and processed.

Six classic threading models (Learn)

Multiple threads share resources in the address space of the same process, which is a simulation of multiple processes on a single computer, sometimes called threads as lightweight processes

For multiple processes on a single computer, you share physical memory, disks, printers, and other physical resources.

Multi-threaded running is similar to running multiple processes, which is a fast switchover between multiple threads by the CPU.

The different processes are full of hostility, each other is preemption, competition CPU relationship, if Thunderbolt will and QQ Rob Resources. And the same process is created by a programmer's program, so the thread within the same process is a partnership, one thread can access the memory address of the other thread, everyone is shared, and one thread dries the memory of another thread, which is purely a problem for the programmer.

Similar to processes, each thread also has its own stack

Unlike processes, line libraries cannot force a thread out of the CPU with a clock interrupt, and can call the Thread_yield run thread to automatically abandon the CPU and let another thread run.

  

Threads are usually useful, but the problem with threading is that it is difficult to design a program:

1. If the parent process has multiple threads, does the open child thread require the same number of threads

If so, a thread in the vicinity is blocked, then copy to the child process, copy version of the thread will be blocked, think of Nginx multithreaded mode to receive user connections.

2. In the same process, if one thread shuts down the problem and another thread is preparing to write content to that file?

If a thread notices that there is no memory and starts allocating more memory, in half of the work, a thread switch occurs, the new thread also discovers that the memory is not enough, and starts allocating more memory, so that memory is allocated multiple times, which are typical problems of multithreaded programming and need to be carefully thought over and designed.

Seven POSIX threads (learn)

In order to implement a portable thread, the IEEE defines the threading standard in IEEE Standard 1003.1C, which defines the thread package called Pthread. Most UNIX systems support this standard and are briefly described below

Eight threads implemented in user space (Learn)

Thread implementations can be divided into two categories: User-level threads (user-level thread) and kernel thread threads (kernel-level thread), which are also known as kernel-supported threads or lightweight processes. In multi-threaded operating system, the implementation of each system is not the same, in some systems to implement the user-level threads, and some systems to implement the kernel-level threads.

User-level thread core switching by the user-state program to control the kernel switch, do not need the core interference, less the consumption of incoming and outgoing kernel state, but not very good use of multi-core CPU, the current Linux pthread generally do so.

The user space simulates the scheduling of the operating system to invoke a thread in a process, and each process has a runtime system to dispatch the thread. At this point, when the process acquires the CPU, the process then dispatches a thread to execute, and only one thread executes at the same time.

Nine threads implemented in kernel space (learn)

Kernel-level thread: The switch is controlled by the kernel, and when the thread switches, the user state is converted to the kernel state. The switch is completed to return the user state from the kernel state, and the SMP can be used to make use of the multi-core CPU. This is the case with Windows threads.

10 user-level vs. kernel-level threading (Learn)

One: The following are the differences between a user-level thread and a kernel-level thread:

    1. Kernel support threads are perceived by the OS kernel, while user-level threads are not perceived by the OS kernel.
    2. The creation, revocation, and dispatch of user-level threads do not require the support of the OS kernel, which is handled at the level of language (such as Java), while kernel support for thread creation, revocation, and dispatch requires the OS kernel to provide support, and is generally the same as process creation, revocation, and scheduling.
    3. When a user-level thread executes a system call instruction, it causes its owning process to be interrupted, while the kernel support thread executes the system call instruction, causing the thread to be interrupted only.
    4. In a system that has only a user-level thread, the CPU is dispatched in a process that is in a running state, and the user program controls the rotation of the thread, and in a system with kernel support threads, the CPU is dispatched in threads, and the thread scheduler of the OS is responsible for the thread's dispatch.
    5. The program entity of a user-level thread is a program that runs under a user-state, while a kernel-enabled program entity is a program that can run in any state.

Two: The advantages and disadvantages of kernel threads

Advantages:

    1. When there are multiple processors, multiple threads of a process can execute concurrently.

Disadvantages:

    1. Dispatched by the kernel.

Three: advantages and disadvantages of user process

Advantages:

    1. Thread scheduling does not require the kernel to participate directly, the control is simple.
    2. Can be implemented in an operating system that does not support threading.
    3. Thread management, such as creating and destroying threads, thread switching costs, and so on, is much less expensive than kernel threads.
    4. Allow each process to customize its own scheduling algorithm, thread management is more flexible.
    5. Threads can take advantage of more table space and stack space than kernel-level threads.
    6. Only one thread is running in the same process, and if one thread is blocking with a system call, the entire process is suspended. Also, a page failure can cause the same problem.

Disadvantages:

    1. Resource scheduling According to the process, multiple processors, a thread in the same process can only be reused under the same processor
11 Hybrid implementations (Learn)

User-level and kernel-level multiplexing, kernel same dispatch kernel thread, each kernel thread corresponding to n user thread

Multi-Threading theory part of Python concurrent programming

Related Article

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.