views:

298

answers:

3

According to MSDN, Sleep() can be provided INFINITE value and that "indicates that the suspension should not time out".

Why would I want to call Sleep() with INFINITE timeout in my program?

+3  A: 

A sleep with no timeout does not need a timer. This reduces the overhead where you anticipate a variable-length wait but are absolutely sure that the thread will be resumed.

Steve De Caux
I don't see how that can be used. The thread calls Sleep( INFINITE ); and freezes forever. How can it be resumed?
sharptooth
Basically you send it an interrupt
Steve De Caux
Emm... How exactly do I do that?
sharptooth
@Sharptooth - why not throw it back to the forum as its own question ?
Steve De Caux
Number one reason is that I have no idea what you're talking about and this actively prevents me from asking a good question.
sharptooth
It's more something used in system programming, I doubt if it's relevant to you. Konami's original answer was pretty good, shame he deleted it.
Steve De Caux
Okay, I see your point now.
sharptooth
+3  A: 

There's no reasons one in his sane mind would ever Sleep(INFINITE). It has no practical meaning.

It is for generality and symmetry to WaitForSingleObject(..., timeout) and SleepEx(timeout), where INFINITE does make sense.

Reminding, that SleepEx will try to consume things out of your thread's APC queue.

Pavel Radzivilovsky
+2  A: 

As far as I know, Sleep, since they introduced SleepEx, it's just a thin, convenient wrapper around SleepEx, and when they rewrote it as a wrapper, they decided just to pass the timeout parameter to SleepEx, without processing it in any way. Obviously in this way the behavior of the function with INFINITE as timeout is propagated also to Sleep (and so must be documented), although, without the bAlertable parameter of the SleepEx, it's completely useless (a Sleep(timeout) is equal to SleepEx(timeout, FALSE), so you'll have an infinite nonalertable wait).

On Windows CE, then, they may have decided to change this behavior because it was actually silly, so I think that a Sleep(INFINITE) on CE is translated automatically to a SuspendThread; however, on Windows they are probably forced to keep the "simple" behavior for compatibility reasons.

Matteo Italia