WaitForSingleObject 是否放弃线程的时间片?
我正在用 C 语言编写一个 win32 程序。
当您运行多个线程,并且其中一个线程正在等待事件(例如使用 WaitForSingleObject()
)时,该线程是否仍能获得其完整的状态? CPU时间片?
换句话说,操作系统是否知道在某个事件发出信号之前线程不需要其时间片?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的——线程被阻塞,直到它正在等待的任何内容收到信号为止。该线程在被阻塞时不会被调度运行,因此其他线程会获得所有 CPU 时间。
请注意,时间片并不会影响太多。线程可以在时间片中间放弃执行,并且(例如)如果它正在等待的内容很快发出信号,它可能会在其原始时间片到期之前再次开始执行。当某件事发出信号时,正在等待的线程可以立即唤醒,而不必等待时间片的结束(例如,如果正在等待的线程比正在等待的线程具有更高的优先级)跑步)。
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.