Concurrent series "sleep(0)"

Operating system knowledge

Before talking about the difference between the two, first review the knowledge of the operating system.

In the operating system, there are many strategies for CPU competition:

  • Unix system uses时间片算法
  • Windows belongs 抢占式to

In the time slice algorithm, all processes are arranged in a queue. The operating system allocates a period of time to each process in their order, that is, the time that the process is allowed to run. If the process is still running at the end of the time slice, the CPU will be deprived and assigned to another process. If the process blocks or ends before the time slice ends, the CPU switches immediately. All the scheduler has to do is maintain a list of ready processes, and when the process runs out of its time slice, it is moved to the end of the queue.

The so-called preemptive operating system means that if a process gets the CPU time, it will completely occupy the CPU unless it gives up using the CPU by itself.

In preemptive operating system, assuming there are several processes, the operating system according to their 优先级, 饥饿时间calculated a total of priority and so on. The operating system will hand over the CPU to the process with the highest overall priority. When the process is finished or hangs on its own initiative, the operating system will recalculate the total priority of all processes once, and then pick the one with the highest priority to give him control of the CPU.

If it is a Unix operating system, each thread is allocated a fixed time slice.

If it is a Windows operating system, because it is based on priority to decide which thread to execute, this may happen, a thread with high priority is executing, and at a certain moment, the thread chooses to suspend according to its own "will" , The priority will be recalculated at this time, and the thread that just chose to suspend has too high a priority before, and the execution time just now is very short, so even if the thread is actively suspended, its priority is still when it participates in the CPU competition again. Is the highest, then the thread will still be selected, but its priority may not be the highest.

Thread.Sleep(0)

So, Thread.Sleepwhat does the function do?

For example, when a thread executes Thread.Sleep(10000)and the thread chooses to suspend for 10 seconds, the operating system will ignore the thread when it recalculates the total priority of all threads in the following 1 second. Tell the operating system “在未来的多少毫秒内我不参与CPU竞争”.

So now there are two questions :

  1. Assuming that the current time is 12:00:00.000, if the call is called Thread.Sleep(10000), that is, the thread is suspended for 10 seconds, then at 12:00:10.000, will the thread be awakened?

    Answer: Not necessarily

    Because you just tell the operating system: I will not participate in the CPU competition in the next 10 seconds. Then after 10 seconds have passed, maybe another thread is using the CPU at this time, then the operating system will not reallocate the CPU until that thread hangs or ends; moreover, even if it happens to be the operating system’s turn to allocate the CPU at this time , Then the current thread is not necessarily the one with the highest total priority, and the CPU may still be preempted by other threads.

    Similarly, Thread has a Resumefunction to wake up a suspended thread. As mentioned above, this function just "tells the operating system that I am participating in the CPU competition from now on". The call of this function does not immediately make this thread gain control of the CPU.

  2. Thread.Sleep(0)The difference between call or not?

    Answer: Yes, and the difference is obvious.

    Thread.Sleep(0)The role of that is “触发操作系统立刻重新进行一次CPU竞争”. The result of competition may be that the current thread still obtains CPU control, or it may be replaced by another thread to obtain CPU control. This is what we often write in large loops Thread.Sleep(0), because this gives other threads such as Paintthreads the power to gain control of the CPU, so that the interface will not be suspended there.

    In addition, although the above mentioned that "unless it gives up using the CPU, it will completely occupy the CPU", but this behavior is still restricted-the operating system will monitor your occupation of the CPU, if it finds that a thread dominates for a long time The CPU will force this thread to suspend, so in fact there will be no situation where "a thread keeps occupying the CPU". As for the program suspended animation caused by our big loop, it is not because this thread has been occupying the CPU. In fact, during this period of time, the operating system has competed for multiple CPUs, but other threads immediately exited within a short time after gaining control of the CPU, so it was the thread's turn to continue executing the loop, and so on again It took a long time to be forced to hang by the operating system. Therefore, when reflected on the interface, it looks as if this thread has been occupying the CPU.

Guess you like

Origin blog.csdn.net/weixin_44471490/article/details/109040689