开发者

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开发者_运维百科?


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).


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜