简体   繁体   English

std :: condition_variable :: wait_until的实现

[英]implementation of std::condition_variable::wait_until

I am reading the libstdc++ implementation of std::condition_variable::wait_until , here is the source: 我正在阅读std::condition_variable::wait_until的libstdc ++ 实现 ,这里是源代码:

template<typename _Clock, typename _Duration>
  cv_status
  wait_until(unique_lock<mutex>& __lock,
     const chrono::time_point<_Clock, _Duration>& __atime)
  {
    // DR 887 - Sync unknown clock to known clock.
    const typename _Clock::time_point __c_entry = _Clock::now();
    const __clock_t::time_point __s_entry = __clock_t::now();
    const auto __delta = __atime - __c_entry;
    const auto __s_atime = __s_entry + __delta;

    return __wait_until_impl(__lock, __s_atime);
  }

template<typename _Clock, typename _Duration, typename _Predicate>
  bool
  wait_until(unique_lock<mutex>& __lock,
     const chrono::time_point<_Clock, _Duration>& __atime,
     _Predicate __p)
  {
    while (!__p())
      if (wait_until(__lock, __atime) == cv_status::timeout)
        return __p();
    return true;
  }

The second function call the first function in a loop.which will do clock sync operation.So if we call the second function,the sync operation may run many times.It is necessary to sync clock every time?I think the code can be improved by sync clock only once in the second function.Am I right? 第二个函数调用循环中的第一个函数。它将执行时钟同步操作。如果我们调用第二个函数,同步操作可能会运行多次。每次都需要同步时钟吗?我认为代码可以改进在第二个功能中只通过同步时钟一次。对吧?

I think you're right. 我觉得你是对的。 On the other hand the assumptions for using wait_until are usually that the event will happen only sparsely and the predicate returning true for all but a few instances of unwanted wake-ups. 另一方面,使用wait_until的假设通常是事件将只是稀疏地发生,并且除了少数不必要的唤醒之外,谓词返回true。 Thus the overhead of re-syncing the clocks should be minimal. 因此,重新同步时钟的开销应该是最小的。 Remember also that in this case the thread had to be woken up and paged in already, which is probably more expensive than querying a clock. 还要记住,在这种情况下,线程必须已经被唤醒并且已经被分页,这可能比查询时钟更昂贵。

Yes, and no. 是的,不。

Yes, this code could be optimized to not manipulate time_point s when it loops. 是的,这个代码可以优化,以便在循环时不操纵time_point However, I'm not sure that's really necessary. 但是,我不确定这是否真的有必要。

Consider what makes the predicated wait_until loop. 考虑是什么使得谓词wait_until循环。

When notified , it checks the predicate to see if there is work to do. 通知后 ,它会检查谓词以查看是否有工作要做。

  1. If the predicate returns true, ie the condition protected by the condition variable is true, wait_until returns true . 如果谓词返回true,即条件变量保护的条件为真,则wait_until返回true
  2. If the timeout elapses, wait_until returns the value of the predicate, which will typically be false (otherwise we would have expected the condition_variable to have have been notified). 如果超时过去, wait_until将返回谓词的值,该值通常为false (否则我们会预期condition_variable wait_until变量已被通知)。

This leaves only one case where the loop actually loops: When the condition_variable is notified, but the predicate returns false . 这只留下一个循环实际循环的情况:当通知condition_variable ,谓词返回false

This is known as a spurious wakeup and is hardly the typical case, so it is not really worth optimizing for. 这被称为虚假唤醒并不是典型的情况,所以它并不值得优化。

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

相关问题 std :: condition_variable :: wait_until函数 - std::condition_variable::wait_until function 如何工作 std::condition_variable::wait_until - How works std::condition_variable::wait_until std :: condition_variable :: wait_for和std :: condition_variable :: wait_until有什么区别? - What is the difference between std::condition_variable::wait_for and std::condition_variable::wait_until? GCC 4.9.2 / GCC 4.8.1-std :: condition_variable :: wait_until(...)错误? - GCC 4.9.2 / GCC 4.8.1 - std::condition_variable::wait_until(…) bug? 系统时间更改的 wait_until 的 condition_variable 解决方法 - condition_variable workaround for wait_until with system time change std :: condition_variable :: wait_until对std :: this_thread :: sleep_for有什么好处吗? - Does std::condition_variable::wait_until have any advantage against std::this_thread::sleep_for? condition_variable::wait_until 意外通过 g++ (9.4.0) - condition_variable::wait_until passes through unexpectedly g++ (9.4.0) C++ 11 - condition_variable - wait_until 没有按预期工作 - C++ 11 - condition_variable - wait_until not working as expected 使用chrono :: steady_clock使用condition_variable :: wait_for和wait_until,但是在PC睡眠时跳过持续时间? - condition_variable::wait_for and wait_until using chrono::steady_clock but skipping duration while PC is asleep? 条件变量wait_until timeout_time过去了? - Condition Variable wait_until timeout_time in the past?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM