The concepts that multithreaded development must know

Source: Internet
Author: User

Operating system

is a computer program that manages and controls computer hardware and software resources, and it is a software.

Multi-tasking

Multitasking is the ability of the operating system to run multiple applications at once. There are two forms of multitasking: collaborative multitasking and preemptive multi-tasking

Collaborative multitasking : The processor can only process one program at a time until the program consumes the processor resources when it is released (after execution exits), and the other running programs get the processor resources. The disadvantage of this multitasking is that if a program does not release processor resources, other applications will be suspended. This is exactly how the earlier versions of Windows are used.

preemptive multi-tasking : The operating system allows an application to execute for a short period of time and then force it to be interrupted for another application to execute. It can be simply understood as interrupting a program to allow another program to execute. It is controlled by the operating system throughout. The benefit of this is to avoid the problem of pending problems with collaborative multi-tasking, but it can cause data synchronization. For example, two applications access and modify a variable at the same time.

Process

When a program is started, the system allocates the required memory and other resources to the program. Physical isolation of memory and resources is called a process. Of course, a program may have more than one process, so programs and processes are not meant to be. The memory that is allocated to the process is isolated from other processes and can only be accessed by itself.

Thread

A thread is the smallest unit that the operating system can perform operations on. It is contained in the process and is the actual operating unit of the process. A thread refers to a single sequential control flow in a process in which multiple threads can be concurrent and each thread performs different tasks in parallel.

Time slices

The time a process can execute is called a "time slice" or "amount of time." The interval between time slices is unpredictable and may be different for each operating system and per processor.

Local Storage (TLS)

When a thread runs longer than its allotted time slice is, it does not stop to wait until it is queued again. Each processor can only process one thread at a time, so when the front thread has to move out of the current processor. However, before the thread jumps out of execution, it has to save the state information before it is left in order to execute again. This feature is called Thread-local storage (the thread locally storage,tls). TCL contains registers, stack pointers, dispatch information, in-memory address space, and other resource information that is in use. One of the registers stored in TLS is the program counter, which is used to tell the thread which instruction to execute next.

Interrupt

Interrupts are a mechanism that enables CPU instructions to be moved from the normal execution order to other places in the computer's memory without needing to know what program is currently executing. Windows knows when it needs to make thread scheduling decisions by interrupting. Windows determines how long a thread executes and puts an instruction in the execution order of the current thread. This time may be different between different systems and even between different threads of the same system. Because interrupts are explicitly placed in the instruction set, they are often referred to as software interrupts. Once the interrupt is set, Windows allows the thread to execute. When a thread executes to an interrupt, Windows uses a specific function called interrupt processing to present the state of the thread in TLS. The program counters for the current thread are also stored in TLS. This program counter is only the address of the current execution instruction. Once the thread execution times out, it is moved to the end of the thread queue at its own priority, waiting to be dispatched again.

Thread sleep and clock interrupts

Threads voluntarily exit the execution queue for a period of time, called sleep. When a thread goes to sleep, it is packaged again into TLS, but this time it is not placed at the tail end of the TLS execution queue. Instead, it is placed in a separate sleep queue. In order for the threads on the sleep queue to run again, they need to be marked with a different interrupt, called a clock break. When a thread enters the sleep queue, it sets a clock interrupt to schedule when to wake the thread. When a clock break occurs, if this time matches the time that a thread on the sleep queue resumes running, the thread goes back to the executable queue and schedules the time to resume execution.

Termination of the thread

The termination of a thread is divided into two situations. One is to explicitly request to stop another thread during one thread execution, and the other is to finish automatically when a thread executes.

Priority of the thread

the priority order of thread execution is called the priority of the thread. Windows divides the priority of threads into 0~31 levels, and the larger the number, the higher the priority. Only the system can set the 0 priority, which indicates that the thread is idle. The user can set the priority of the 1~15, and only the system administrator can set a priority higher than 15. The thread running between 16~31 is a real-time thread. "Real-time" refers to the very high priority that can preempt the execution time of a low-priority thread.

The concepts that multithreaded development must know

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.