简体   繁体   English

使用std :: condition_variable和std :: timed_mutex

[英]using std::condition_variable with std::timed_mutex

Is it possible? 可能吗? I want to use timed_mutex instead of a regular mutex with a condition_variable, but it won't compile and looking at sources 我想使用timed_mutex而不是带有condition_variable的常规互斥锁,但是它不会编译并查看源代码

  void
  wait(unique_lock<mutex>& __lock, _Predicate __p)
  {
while (!__p())
  wait(__lock);
  }

(indentation courtesy of libc++ authors, really?) (缩进由libc ++作者提供,真的吗?)

So it looks like it is in fact limited to straight mutexes, not timed ones. 因此,看起来它实际上仅限于直接互斥,而不是定时互斥。 But why?? 但为什么??

Yes, std::conditional_variable is limited to std::unique_lock<std::mutex> . 是的, std::conditional_variable仅限于std::unique_lock<std::mutex> However you can use the more generic std::condition_variable_any with anything that has a compatible interface . 但是,您可以将更通用的std::condition_variable_any用于具有兼容接口的任何东西。

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

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