WaitForSingleObject是否放弃线程的时间片? [英] Does WaitForSingleObject give up a thread's time slice?

查看:322
本文介绍了WaitForSingleObject是否放弃线程的时间片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用C编写一个win32程序.

I'm making a win32 program in C.

当您有多个正在运行的线程,并且其中一个线程正在等待事件(例如,使用WaitForSingleObject())时,该线程是否仍获得其完整的CPU时间片?

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?

推荐答案

是的-线程被阻塞,直到发出等待信号为止.该线程在被阻止时不会安排运行,因此其他线程将获得所有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).

这篇关于WaitForSingleObject是否放弃线程的时间片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆