. NET Foundation Supplements (7) Multithreaded Development Fundamentals 1

Source: Internet
Author: User

First, the basic concept of multithreaded Programming 1.1 OS-level processes and threads

(1) Process

The process represents an application running on the operating system . The process has its own block of programs , has exclusive resources and data, and can be dispatched by the operating system.

But even if the same application is forced to start multiple times, it will be placed in separate processes and run separately.

The best way to intuitively understand a process is through the process manager, where each record represents an active process:

(2) thread

  A thread is sometimes called a lightweight process, a unit that can be dispatched and maintains its own stack and context .

A thread is attached to a process, a process can contain 1 or more threads, and multiple threads within the same process share a chunk of memory and resources .

As a result, a thread is a basic unit that the operating system can dispatch , but its dispatch is constrained by the process that the thread belongs to, that is, the operating system first decides to execute the next executing process, and then the thread within that process is dispatched. The basic life cycle of a thread is as follows:

(3) The difference between a process and a thread

The biggest difference is in isolation , where each process is isolated (process crashes do not affect other processes, so interactions between processes are relatively difficult)

All threads in the same process share memory and resources, and one thread can access and end other threads within the same process.

Are 1.2 multithreaded threads executed in parallel in the operating system?

(1) Scheduling of Threads

In the early days of the development of computer systems, there was no parallel concept at the operating system level, and all applications were queued for a single-threaded queue, and a small error would cause all programs on the operating system to block.

In the later operating system, the concept of time-sharing and process and thread were gradually generated. Multiple threads are scheduled and controlled by the operating system, deciding when to run which thread.

The so-called thread scheduling, refers to the operating system determines how to arrange the order of thread execution algorithm . According to the general classification, thread scheduling can be divided into the following two kinds:

① preemptive scheduling

Preemptive scheduling means that each thread has very little running time (not more than 20msin Windows NT kernel mode), and when the time slice runs out, the thread is forced to pause, save the context, and give the running right to the next thread. The result of this dispatch is that all threads are constantly switching on and off, making the user feel that all the threads are running in parallel .

② Non-preemptive scheduling

Non-preemptive scheduling means that a thread is not forced to suspend by the operating system at run time, it can run continuously until the run is over and voluntarily surrender the run. In this way, the running of a thread is a single queue, and it is possible for a malicious program to monopolize the operation for a long time.

PS: many OSes, including Windows, now employ both preemption and non-preemption modes. The higher priority thread OS uses non-preemption to give sufficient time to run, while for normal threads the preemptive mode is used to quickly switch execution.

(2) Thread parallel problem

In a single-core single-CPU hardware architecture, the parallel running of threads is a user's subjective experience. In fact, there may only be one thread in the running state at any one time. However, in a multi-CPU or multi-core architecture, the situation is slightly different. A multi-CPU multi-core architecture allows the system to run two or more threads with no other resource contention in full parallel, in theory such architectures can increase the performance of an integer multiple.

. NET Foundation Supplements (7) Multithreaded Development Fundamentals 1

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.