简体   繁体   English

C++ 唤醒一个休眠线程?

[英]C++ Wake up a sleeping thread?

There's a ton of questions and answers already ( Exhibit A ) about how to wake up a sleeping thread from another thread or how to have a thread sleep until it receives an instruction (same thing but described from another direction), but my question is more specifically about what is actually happening under the hood.关于如何从另一个线程中唤醒一个正在休眠的线程或如何让一个线程休眠直到它收到一条指令(同样的事情,但从另一个方向描述),已经有很多问题和答案( 图表 A ),但我的问题更多特别是关于幕后实际发生的事情。

Is the only way to do this for the thread to itself choose to sleep and then check its queue, or some variable somewhere when it wakes, then sleep again?这样做的唯一方法是让线程自己选择休眠然后检查它的队列,或者当它醒来时某个地方的某个变量,然后再次休眠?

Another method that I've seen mentioned is to use a mutex.我见过的另一种方法是使用互斥锁。 Have the thread try to lock a locked mutex and it'll sleep then magically wake up when the mutex is unlocked by its original locker.让线程尝试锁定一个锁定的互斥锁,它会休眠,然后当互斥锁被其原始锁解锁时神奇地唤醒。 But isn't that just doing the same thing under the hood?但这不就是在幕后做同样的事情吗? Ie going to sleep for x period of time then waking up at intervals to check if its still locked?即要睡x一段时间,然后每隔一段时间醒来检查它是否仍然锁定?

Is there any kind of wake functionality that doesn't require sleeping for set intervals?是否有任何类型的唤醒功能不需要在设定的时间间隔内休眠? Like some kind of OS-level push?像某种操作系统级别的推送? For example, the OS must be keeping track of when it needs to wake the sleeping thread, right?例如,操作系统必须跟踪何时需要唤醒睡眠线程,对吗? So in theory if you could change that number to 0 or 1 then it would wake up sooner.所以理论上如果你能把这个数字改成 0 或 1,那么它会更快醒来。 That makes sense.那讲得通。

The reason why I ask is because there's a tradeoff between how long it takes to wake up, and the amount of resources its using.我问的原因是因为在唤醒需要多长时间和它使用的资源量之间需要权衡。 The longer it sleeps between checks, the slower it is to respond, and the shorter it waits the more cycles it wastes checking something that another part of the application already knows has not changed.它在检查之间休眠的时间越长,响应就越慢,它等待的时间越短,它浪费的周期就越多,它浪费在检查应用程序的另一部分已经知道没有改变的东西上。 Seems inefficient.似乎效率低下。

You are misunderstanding condition variables.您误解了条件变量。 The thread in the first example in this answer will wake up after 1000 seconds or when the condition variable is notified .此答案中第一个示例中的线程将在 1000 秒后或通知条件变量时唤醒。 There is no tradeoff.没有权衡。

Likewise, a thread that waits for a mutex with a 1000-second timeout will wake up after 1000 seconds, or when the mutex is available and the thread acquires it .同样,等待具有 1000 秒超时时间的互斥锁的线程将在 1000 秒后唤醒,或者当互斥锁可用并且线程获取它时唤醒。

You don't have to set low timeouts to get fast wakeups.您不必设置低超时来获得快速唤醒。

But isn't that just doing the same thing under the hood?但这不就是在幕后做同样的事情吗? Ie going to sleep for x period of time then waking up at intervals to check if its still locked?即要睡x一段时间,然后每隔一段时间醒来检查它是否仍然锁定?

No, it isn't.不,不是。 It tells the OS to wake it up whenever it's unlocked or after 1000 seconds.它告诉操作系统在解锁时或 1000 秒后唤醒它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM