WaitForSingleObject 是否放弃线程的时间片?

发布于 2024-10-02 04:55:22 字数 172 浏览 7 评论 0原文

我正在用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

顾冷 2024-10-09 04:55:22

是的——线程被阻塞,直到它正在等待的任何内容收到信号为止。该线程在被阻塞时不会被调度运行,因此其他线程会获得所有 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).

清音悠歌 2024-10-09 04:55:22

如果您正在等待的对象尚未收到信号,则线程将放弃其剩余的时间片并进入睡眠状态,直到该对象收到信号。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文