首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >boost::interprocess::interprocess_condition::timed_wait永远在等待

boost::interprocess::interprocess_condition::timed_wait永远在等待
EN

Stack Overflow用户
提问于 2020-03-23 19:12:57
回答 1查看 65关注 0票数 1

我有以下最小的例子:

代码语言:javascript
复制
#include <iostream>
#include <boost/thread.hpp>
#include <boost/interprocess/sync/interprocess_condition.hpp>
#include <boost/interprocess/sync/interprocess_mutex.hpp>


int main(int argc, char* argv[]) {
  boost::interprocess::interprocess_condition ic;
  boost::interprocess::interprocess_mutex im;
  bool test = false;
  auto testFunction = [&ic, &im, &test]() {
    boost::unique_lock lk(im);
    auto tin = boost::posix_time::microsec_clock::local_time();
    auto waitTime = tin + boost::posix_time::milliseconds(100);
    if (!ic.timed_wait(lk, waitTime)) {
      test = false;
    }
    else {
      test = true;
    }
  };

  auto notifyFunction = [&ic]() {
    ic.notify_all();
  };

  boost::thread t(testFunction);
  boost::this_thread::sleep_for(boost::chrono::milliseconds(2000));
  boost::thread t2(notifyFunction);
  t.join();
  t2.join();
  std::cout << "Result is: " << std::boolalpha << test << std::endl;
  return 0;
}

我所期望的是,在lambda函数中,interprocess_condition变量ic等待100毫秒,然后函数继续执行。相反,该条件等待在第二个线程中调用的通知。

我做错了什么?我应该如何正确使用timed_wait才能等待所需的时间?

我使用的是boost版本1.72.0。

EN

回答 1

Stack Overflow用户

发布于 2020-03-23 19:29:48

我已经找到解决方案了。我需要使用boost::posix_time::microsec_clock::universal_time而不是boost::posix_time::microsec_clock::local_time

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60812449

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档