Click to See Complete Forum and Search --> : Waking up a sleeping thread


mdv
August 15th, 2007, 11:11 AM
Hello,

Is there a way to immediately wake up a thread that was put to sleep by calling the Sleep function with some big value?

Thanks in advance. Denis

JVene
August 15th, 2007, 01:13 PM
Not with sleep, and immediately is relative to the task scheduler's switching speed.

Use an event, probably an auto reset event, and wait on the event with a long time value (compatible with sleep's value).

When you signal the event, the thread will jump into action.

Now, about immediately - there is no immediately, with the possible exception of a real time OS. Once an event is signaled, the OS will place that thread back into the schedule for the task switcher to serve some time to the thread. The thread will begin executing once the task scheduler gets around to serving that thread, which depends on how busy the system is already, how many CPU's are in the machine, the priority of the thread in question, etc.

On typical modern Windows systems, as an example, the task switching rate is in the region of 150,000 switches per second. This is closer to 'immediate' that most 'real time' games, but with respect the amount of work a CPU can accomplish in 1/150,000th of a second (or, if the system is quite busy, perhaps as long as 1/1000th of a second, or even longer), the delay represents some considerable potential. It is usually negligible, but I make note of it because for some applications it can be of some significance.

mdv
August 16th, 2007, 02:22 AM
Yep, having an event is a good way to go in this situation - thanks for the advice.

In MSDN it is said that on WinCE platforms calling the Sleep function results in an internal call of SuspendThread, thus it is possible to wake up a sleeping thread by calling ResumeThread. It is also said it's not the case for the WinNT branch systems - I'm wondering why is it so (does it have something to do with the real-time nature of the WinCE OS)?

Denis

Arjay
August 20th, 2007, 11:20 AM
Yep, having an event is a good way to go in this situation - thanks for the advice.

In MSDN it is said that on WinCE platforms calling the Sleep function results in an internal call of SuspendThread, thus it is possible to wake up a sleeping thread by calling ResumeThread. It is also said it's not the case for the WinNT branch systems - I'm wondering why is it so (does it have something to do with the real-time nature of the WinCE OS)?

DenisThe WinCE docs state:

"Calling Sleep(INFINITE) is different on Windows CE-based platforms than it is for Windows-based desktop platforms. For Windows CE-based platforms, Sleep(INFINITE) is equivalent to calling SuspendThread(GetCurrentThread()). This means that the thread suspend count is incremented from 0 to 1 and can be resumed, or woken up, by another thread that calls ResumeThread on the sleeping thread. A Sleep(INFINITE) call on Windows-based desktop platforms is not a SuspendThread call, and calling ResumeThread on the sleeping thread does not resume the thread."

I interpret this to mean that you can only wait up the thread in WinCE using ResumeThread if you used Sleep(INFINITE), but not after calling Sleep with a non-infinite timeout.