Threads I know

Source: Internet
Author: User

Opening

1. Background

For a long time ago, with the development of processing technology, the CPU processing speed has been improving, basically doubling every 18 months. Since the CPU clock speed reached GH in 04 years, this rule seems to have expired because people have reached the physical limit in manufacturing CPU technology. CPU processing speed can be further improved unless there is a breakthrough in technology. However, the amount of data to be processed does not stop increasing. One of the methods is to use multi-core and parallel processing technologies. This will become and will become the future development trend. To understand the parallel technology, it is necessary to have a certain understanding of the thread. This blog mainly talks about my views on threads. This is just a simple point of view.ArticleThe author's cognition is limited, and I still hope to correct the shortcomings.

2. My thoughts

With regard to concurrent programming, I think it would be the best solution to have a language specifically designed for parallelism. Because most of the existing languages target single-core processors, most of the current concurrency is completed by the operating system, and the concurrency is written in the current language.ProgramThe technology is not very mature yet. If there is a language specifically designed for concurrency, it will greatly improve the program running efficiency. Of course, this is just my small idea.

3. Content Overview:

1) conceptual differences between programs and processes

2) thread Concept

3) multi-thread scheduling

4) thread security issues (when multiple threads simultaneously access a variable)

Programs and processes

Generally, programs can be considered executable files on specific operating systems. That is, the executable file formed by the source code being compiled and linked depends on the operating system of the execution, because the operating system provides the environment for running the program (Runtime Library, etc ). It is a static concept.

A process is a dynamic concept. It has its own address space and can perform some operations. The execution of a program is accompanied by the generation of a process. The execution of a program produces one or more processes.

Therefore, processes can be considered as dynamic concepts of programs.

Summarize the differences between programs and processes

1) The process is dynamic, and the program is static.

2) A process has a life cycle, while a program is a set of commands and has no "motion" meaning. Programs without processes cannot be recognized by the operating system as a separate unit.

3) one program can generate multiple processes. One process corresponds to only one program.

What is thread

A thread, also known as a lightweight process, is the smallest unit of program execution. As mentioned above, a process can correspond to multiple threads, and a thread only belongs to one process.

The execution of a process is based on threads. For example, a simple "Hello World" program has only one thread, that is, the thread corresponding to the main () function.

Thread composition:

1) thread ID. Used for standard thread

2) The current command pointer is PC. Indicates the execution point of the next command

3) register set and stack. Available space for this thread

Multithreading

In most software applications, there are more than one thread. Multi-thread execution can be performed concurrently, and the global data of the process is shared.

Advantages of multithreading:

1) An operation may be waiting for a long time. Multithreading is used. When a thread waits, other threads can be executed to make full use of the CPU.

2) an operation (such as computing) may consume a large amount of time, resulting in interruption of interaction with users. Multithreading allows one thread to take charge of computing and the other thread to take charge of interaction.

3) the software itself requires concurrent operations, such as multi-port Software downloading

4) multi-core CPU, capable of executing multiple threads at the same time

Thread access permission

Generally, threads can access all data in the process memory, but in actual applications, threads also have their own space.
1) stack (may be accessed by other processes, but can still be considered as private data)

2) local thread storage, usually with a small capacity

3) registers (including PC registers)

Thread Scheduling

The best case is that when the number of processors is greater than the number of threads to be processed, all threads can be executed simultaneously. Implement true concurrency.

This is basically impossible in reality. In reality, concurrency is only a simulated state, especially when single-core processing is used for multithreading. It allows multiple threads to execute alternately, and each thread executes for a short time. On the surface, these threads execute simultaneously to achieve concurrency.

Every thread wants to be executed, but the number of threads for each execution is limited, so there is a way to select the thread to be executed from a large number of threads, the single-core architecture is similar to that of multi-core architecture.

Dedicated thread scheduling in the operating systemAlgorithmBelow are a few simple "scheduling algorithms"

1. "first-in-first-out" strategy.All threads form a queue. New threads are added to the end of the queue, and each time the queue header is taken for execution. One drawback is that, if the new thread is an emergency operation, the operating system needs to respond as soon as possible, and this scheduling method cannot be satisfied.

2. Scheduling by priority.Each thread has its own priority and can be modified by the operating system. During scheduling, the execution with the highest priority is selected. This method makes up for the defects of the previous method. For emergencies that require timely response, you can give them a high priority so that they can be scheduled next time. However, this method also has a problem, that is, the so-calledHunger. If a thread "seems" irrelevant and is given a low priority, the priority of the thread generated each time will be higher than that of the thread, the thread will never be executed and become starved to death. One solution is to increase the thread priority over the event. In this way, as long as the event is long enough, the thread with a lower priority will be executed with a higher priority.

From the above analysis, the thread seems to have two states: Execution and not execution (waiting ). In fact, each thread in the operating system should be in three states.

Thread status:

As mentioned above, only a ready thread (called a ready thread) can be scheduled. After scheduling, the thread is executed in the processor, and the thread is in the running state. If the thread is waiting for the occurrence of an event (such as the response I/O), this State becomes waiting.

To sum up the three states of a thread:

1) Ready: the thread can run I immediately (if this thread is called)

2) Run: The thread is executing

3) Waiting: The thread is waiting for the occurrence of something to continue execution.

In Windows, the thread status is:

Initialized indicates that the internal state of a thread object has been initialized, which is an internal state during thread creation. At this time, the thread has not been added to the thread linked list of the process and has not been started.
.
When a thread is running, it only considers the thread in the ready state. At this time, the thread has been added to the ready thread linked list of a processor.

Running: The thread is running. This thread occupies the processor until the time limit is reached, or is preemptible by a higher-priority thread, or the thread stops, or voluntarily gives up the processor execution right, or enters the waiting state.

Standby: the standby thread has been selected as the next thread to run on a processor. For each processor in the system, only one thread can be in the STANDBY state. However, a thread in the STANDBY state may be preemptible by a thread with a higher priority before it is actually executed.

Terminated: indicates that the thread has completed the task and the resource is being recycled. The keterminatethread function is used to set this status.

Waiting: A thread is waiting for a condition. For example, waiting for a distributor object to become a signal State can also wait for multiple objects. When the waiting conditions are met, the thread can start running immediately or return to the ready state.

Transition: The transferred thread is ready to run, but its kernel stack is not in the memory. Once its kernel stack is switched into memory, the thread enters the ready state.

Deferredready: a thread in the delayed readiness state is ready to run. However, unlike the readiness state, it is not yet determined on which processor to run. When there is a chance of being scheduled, it can be directly transferred to the STANDBY state or switched to the ready state. Therefore, this state is introduced for multi-processor purposes and has no significance for single-processor systems.

Wait: The thread is waiting for a door object. This status is similar to the waiting status, but it is designed specifically for the door object.

The conversion between them is shown below:

For more information about scheduling, see modern operating system and Windows Kernel Analysis.

Preemptible and non-preemptible threads

A thread that can be preemptible means that after the thread runs out of its own time slice, the operating system will forcibly cut the thread out to execute other threads.

A thread that cannot be preemptible cannot be forcibly cut out, unless it gives up the CPU right and terminates the thread, rather than force cut out by the use of time slices. The thread switching time of a thread that cannot be preemptible is determined. This occurs when the thread voluntarily switches out.

Thread Security

When multiple threads are concurrent, some problems may occur in accessing data. Especially when multiple threads access the same variable.

The following uses an example to illustrate possible problems:

Thread a and thread B operate variable X in the following sequence:

1) thread a assigns a value to X.

2) thread a automatically adds x

3) thread B uses the value of X (for example, assign it to another variable)

CodeAfter compilation, when code is executed in a processor, a simple operation (such as the operation itself) is usually divided into multiple steps for execution (command flow ).

For example, when thread a performs an auto-increment operation on X, the compiled auto-increment operation is divided into three steps. When the three steps are not completed, thread A may be cut out (for example, an operation that requires immediate response ). That is to say, thread a is cut out when the data has not been fully processed. In this way, when thread B executes, the X value used will not be the expected value. Obviously, an error occurred.

Solution policy:

The problem above is essentially because an operation that should not be interrupted is forcibly interrupted, so one solution is to set up some operations and cannot be interrupted during execution. In this way, the success of the operation is avoided. These simple operations are called atomic operations. Windows also supports this operation.

However, this policy is only applicable to simple scenarios. In complex cases, we use a mechanism called synchronization and lock.

Synchronization and lock

To put it simply, before a thread finishes its access to data, other threads cannot access the data. In this way, access to data will be atomic.

The implementation of this mechanism is also very simple: Every thread tries to obtain the lock when accessing the data, and is released after the access is complete. When the lock is obtained, if a thread is accessing the data, the data will fail to be obtained. At this time, the thread will wait until the thread accessing the data releases the lock.

Binary semaphores

Is the simplest lock. It has only two statuses: occupied and not occupied. It is used for resources that can only be accessed by one thread. The resource status can be obtained by the thread only when the resource status is not occupied. After the resource status is obtained, the resource status is changed to occupied. After the access, the resource status is changed to not occupied.

Semaphores

It is a little more complicated. It is suitable for resources that can be accessed by multiple threads at the same time. A signal with an initial value of N = N can be simultaneously accessed by n threads. When you want to access data, first check the value of N (the value of N indicates how many threads can access resources). If the value of N is greater than 0, this thread can access resources, after the thread enters, the N value is reduced by one. When the access ends, the N value is + 1.

If the semaphore value is less than 0, it enters the waiting state.

Mutex

Similar to a binary signal, a resource can only be accessed by one thread, but the same semaphore can only be released by the thread that obtains the semaphore. That is to say, for a binary semaphore, the same mutex can be set to another (arbitrary) the thread is released. Stricter than binary semaphores.

Critical Section

It is a more rigorous means of synchronization than mutex. The difference between the critical section and the preceding section is that mutex and semaphores are visible in any process. That is to say, a process creates mutex and semaphores, which are visible in other processes, the scope of the critical section is limited to this process.

Read/write lock

Read/write locks are used in more general scenarios. For the same data, it is okay to read multiple processes at the same time. However, if a thread needs to modify the data, it is necessary to use synchronization methods to avoid errors. There are two methods to obtain the same read/write lock:

1. Shared, read-only data, can be performed by multiple threads at the same time

2. Exclusive. Data will be modified. No other thread operation data is available before the modification is complete.

When the lock is in the Free State, any way to obtain the lock will be successful, and the lock as the corresponding state.

If the lock is in the shared state, all other threads that obtain the lock in the shared mode can successfully read the data together.

For exclusive access, it can be obtained only after all threads acquired in the shared mode are released (the lock returns to the Free State. Any other requests to the lock will not succeed for the lock obtained in an exclusive manner.

Condition variable

The condition variable is similar to a starting gun. Multiple Threads can wait for the gun to ring. When the gun rings, the threads waiting for the gun to ring will resume execution at the same time. The thread determines when the starting gun will sound.

That is to say, the condition variable can let multiple threads wait for a thing to happen. When the time occurs (the condition variable is awakened), All threads can resume execution together.

Summary

This article mainly introduces the concept of thread, briefly introduces thread scheduling, and finally describes the thread security issues, that is, related issues when multiple threads share resources. The actual example is not provided here. This is because there are already many online thread instances.

The purpose of this article is to summarize my understanding of the thread and understand the thread from my perspective. I hope this article will help you.

 

 

See: Computer Systems Windows Kernel Analysis, programmer self-cultivation

If there is reprint please indicate the source: http://www.cnblogs.com/yanlingyin/

A fish ~ @ Blog 2-12

E-mail: yanlingyin@yeah.net

 

 

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.