简体   繁体   English

std :: condition_variable wait_for无限

[英]std::condition_variable wait_for infinite

I am trying to make an infinite wait on the condition variable with the following code (just a sample to display the issue): 我试图使用以下代码对条件变量进行无限等待(仅显示问题的示例):

std::condition_variable cond;
std::mutex mtx;
std::unique_lock<std::mutex> lock(mtx);
cond.wait_for(lock, std::chrono::steady_clock::duration::max());

But the wait exits straight away. 但等待会立即退出。 Digging a little deeper into the (MS) implementation of wait_for I've found that it actually uses wait_until function. 深入研究wait_for的(MS)实现我发现它实际上使用了wait_until函数。 But before that it converts the time using a call to chrono::system_clock::now() and just adding the duration. 但在此之前,它使用对chrono::system_clock::now()的调用来转换时间,并只添加持续时间。

And of course this leads to integer overflow so the new time becomes <= 'now'. 当然,这会导致整数溢出,因此新时间变为<='now'。 Thus wait_until exits immediately. 因此wait_until立即退出。 The same happens to all other timed wait functions (eg try_lock_for in std::timed_mutex class). 所有其他定时等待函数也是如此(例如std::timed_mutex类中的try_lock_for )。

Summurazing the above, I would like to ask if this is a bug in the implementation of the timed wait functions and if so then where can I write about it? 对上面的内容进行总结,我想问一下这是否是定时等待函数的实现中的一个错误,如果是,那么我可以在哪里写一下呢?

Also, since wait_until uses system_clock , actual wait time should vary if there were time adjustments during the wait (because system_clock is not monotonic). 此外,由于wait_until使用system_clock ,如果在等待期间有时间调整(因为system_clock不是单调的),实际等待时间应该会有所不同。 Thus there is no trust to wait duration. 因此,没有信任等待持续时间。

The cppreference documentation says: cppreference文档说:

Note that rel_time must be small enough not to overflow when added to std::chrono::steady_clock::now() . 需要注意的是rel_time必须足够小加的时候不要溢出std::chrono::steady_clock::now()

And: 和:

The clock tied to timeout_time is used, which is not required to be a monotonic clock.There are no guarantees regarding the behavior of this function if the clock is adjusted discontinuously, but the existing implementations convert timeout_time from Clock to std::chrono::system_clock and delegate to POSIX pthread_cond_timedwait so that the wait honors ajustments to the system clock, but not to the the user-provided Clock. 使用与timeout_time相关的时钟,这不需要是单调时钟。如果时钟不连续调整,则无法保证此函数的行为,但现有实现将timeout_time从Clock转换为std::chrono::system_clock并委托给POSIX pthread_cond_timedwait以便等待调用系统时钟,而不是用户提供的时钟。 In any case, the function also may wait for longer than until after timeout_time has been reached due to scheduling or resource contention delays. 在任何情况下,由于调度或资源争用延迟,该函数也可能等待超过达到timeout_time之后的时间。

Even if the clock in use is std::chrono::steady_clock or another monotonic clock, a system clock adjustment may induce a spurious wakeup. 即使使用的时钟是std::chrono::steady_clock或另一个单调时钟,系统时钟调整也可能引起虚假唤醒。

If you want an infinite timeout you can just do something like this (not tested): 如果你想要无限超时,你可以做这样的事情(未经测试):

wait_until(lock, std::chrono::sys_time::max());

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

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