问题
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?
回答1:
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).
回答2:
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.
来源:https://stackoverflow.com/questions/4170272/does-waitforsingleobject-give-up-a-threads-time-slice