Difference between sleep and waitforsingleobject

Source: Internet
Author: User
Tags sleep function
1. Interpretation of msdn sleep

This function suspends the execution of thecurrent thread for a specified interval.

The specified time interval for terminating a thread.

Void sleep (

DWORD Dwmilliseconds);

Parameters

Dwmilliseconds

Specifies the time, in milliseconds, forwhich to suspend execution. A value of zero causes the thread to relinquish theremainder of its time slice to any other thread of equal priority that is readyto run. if there are no other
Threads of equal priority ready to run, thefunction returns immediately, and the thread continues execution. A value ofinfinite causes an infinite delay.

The specified suspend execution time is millisecond. 0 causes the thread to discard its remaining time slice and turn to another thread with the same priority to be executed. If other threads without the same priority are running, the function returns immediately and the current thread continues to execute. Infinit causes infinite latency.

Void sleep (dworddwmilliseconds); this function allows the thread to pause its own running until dwmilliseconds ends.

The sleep function has the following important issues worth noting:

* When sleep is called, the thread can voluntarily discard its remaining time slice.

* The system will make the thread unschedulable within a specified millisecond.

* You can call sleep and pass infinite to the dwmilliseconds parameter. This tells the system never to schedule this thread. This is not something worth doing. It is best to let the thread exit and restore its stack and kernel objects.

* You can pass 0 to sleep. This tells the system that the calling thread will release the remaining time slices and force the system to schedule another thread. However, the system can reschedule the thread that just called sleep. This problem occurs if there are no scheduling threads with the same priority.

Ii. Differences between sleep and waitforsingleobject

Sleep is only used to sleep for a period of time. Once the time reaches, the sleep will become a schedulable state. While waitforsingleobject is to wait for a kernel object to become in a signal State. Its purpose and significance are obvious, so it is more targeted. In my personal habits, waitforsingleobject is not needed in the main thread, because it is prone to deadlock.

Waitforsingleobject

Sleep

Wait for the signal to return,

Wait for a certain amount of time

Flexible; the signal may be returned in advance during the waiting time.

You need to force wait for a fixed time

 

Conclusion: If a message-driven API may be involved in a working thread, the preceding scheme must be used instead of waitforsingleobject functions in the main thread. [This type of problem has not been encountered].

However, the [msdn] provides the following details:

IFA thread creates any windows, it must process messages. message broadcasts aresent to all windows in the system. A thread that uses a wait function with notime-out interval may cause the system to become deadlocked.
Two examples ofcode that indirectly creates windows are DDE and COMCoinitialize. Therefore, if you have a thread that creates windows, use
MsgwaitformultipleobjectsOrMsgwaitformultipleobjectsex, Rather
Waitforsingleobject.

If the thread creates a window, it must process the message. Message broadcasts are sent to all windows in the system. A wait function without delay interval may cause a system deadlock. The window is indirectly created during DDE and COM Initialization. Therefore, if a window is created through a thread, useMsgwaitformultipleobjectsOr
MsgwaitformultipleobjectsexInsteadWaitforsingleobject..

I borrowed some replies from the Forum. Thank you!

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.