The meaning of the sleep () function

Source: Internet
Author: User

===============windows Platform: ======================

about void Sleep (DWORD dwmilliseconds); functions, many people feel that it is telling the system how many milliseconds to delay to execute the following code.

However, in a non-real-time multitasking system such as windows, we cannot estimate how long we will sleep. Because a thread is not always executed. It is suspended by the system after a period of execution, and then the system executes another thread code. As a result, the sleep function is interpreted as follows:

When the sleep function is called, such as sleep (400), it tells the system that the thread will discard the time slice of the run, for example, now that the thread is only executing 10ms, and that the "department rules" it will be woken up once to execute 20ms. Then it said, this opportunity I give up, the back of the 10ms don't. I'll call me again on the next round. In this way, the system terminates it and then schedules the selection again. If it's good luck and is selected, the system will see if the thread is in the sleep flag. If you find that he still needs to continue to sleep, reschedule the selection until you select a thread that has permission to execute. If unfortunately, 400ms arrived, but the system is very busy, scheduling algorithm for a long time also did not choose to this thread, so the thread will continue to hibernate. Thus, this sleep (400) will cause this thread to hibernate at a time greater than or equal to 400MS.

I used sleep as a way to control the speed of program execution. It's really a big debt to think about it now.

More authoritative explanations for this function (transcription):

1. Call sleep to allow the thread to voluntarily abandon its remaining time slices.

2, the system will be in about a specified number of seconds to make the thread unenforceable. Good, if tell the system, want to sleep 100MS, then can sleep about so long time, but also may sleep several seconds or several minutes. Remember, Windows is not a real-time operating system. Although a thread may be awakened at a specified time, it can do so depending on what else is going on in the system.

3. You can call sleep (INFINITE) to tell the system never to call this thread. But it's better not to, let the thread quit on the line. You don't need it anyway. or call Sleep (0), tell the system thread to discard the remaining time slices, and have the system dispatch another thread. However, the system can reschedule the thread that just called sleep. If there are no more than one scheduled thread that has the same priority. This can happen.

The meaning of the sleep () function

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.