C++ boost::condition::timed_wait 的使用示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7078511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:26:56  来源:igfitidea点击:

Usage example of boost::condition::timed_wait

c++multithreadingboostboost-threadcondition-variable

提问by Cookie

Does someone have an example of how to most easily use boost::condition::timed_wait? There are some threads on the topic here, hereand here, but none feature a working example. And boost doc is as usual quite sparse.

有人有一个如何最轻松地使用 boost::condition::timed_wait 的例子吗?此处此处此处有一些关于该主题的主题,但没有一个提供工作示例。boost doc 和往常一样非常稀疏。

回答by Cookie

Actually, I finally found a link with full example here. With a bit of adapting, this seems to be the call.

实际上,我终于在这里找到了完整示例的链接。稍加调整,这似乎就是决定性的。

boost::system_time const timeout=boost::get_system_time()+ boost::posix_time::milliseconds(35000);
boost::mutex::scoped_lock lock(the_mutex);
if(the_condition_variable.timed_wait(lock,timeout,&CondFulfilled))
{
    <cond fulfilled code>
}
else
{
    <timeout code>
}
bool CondFulfilled() { ... }