Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does WaitForSingleObject give up a thread's time slice?

I'm making a win32 program in C.

When you have multiple threads running, and one of the threads is waiting for an event (using WaitForSingleObject() for example), does that thread still get its full CPU time slice?

Stated differently, does the operating system know that the thread doesn't need its time slice until one of the events is signalled?

like image 297
Dean Avatar asked Nov 13 '10 00:11

Dean


2 Answers

Yes -- the thread is blocked until whatever it's waiting on becomes signaled. The thread won't be scheduled to run while it's blocked, so other threads get all the CPU time.

Note that time slices don't enter into it much though. A thread can give up execution in the middle of a time slice, and (for example) if what it's waiting on becomes signaled quickly, it might start executing again before its original time slice expires. When something is signaled, a thread that's waiting on it can wake up immediately, not necessarily waiting for the end of a time slice (e.g., if the thread that was waiting has higher priority than the thread that was running).

like image 144
Jerry Coffin Avatar answered Oct 24 '22 23:10

Jerry Coffin


If the object you're waiting on is not already signalled, the thread will yield the rest of its timeslice and go to sleep until the object is signalled.

like image 26
Gabe Avatar answered Oct 24 '22 23:10

Gabe