c++ - 创建 scoped_lock 最多 200 毫秒

标签 c++ boost boost-interprocess boost-mutex scoped-lock

我正在尝试在互斥锁上创建一个定时作用域锁。我认为遵循 boost 的 api 可能会有所帮助,但我很难找到一些示例代码作为使用它的引用。

scoped_lock(mutex_type & m, const boost::posix_time::ptime & abs_time);

我想做这样的事情,

scoped_lock<interprocess_mutex> lock (_mutex,  ? /* lock for 200 ms max */ ) ;

我的问题是如何创建持续时间为 200 毫秒的 posix_time::ptime 对象。我试图创建 time_duration 对象,但他们没有直接接受它的构造函数。

boost::posix_time::time_duration td = boost::posix_time::milliseconds(200);

有人可以帮我理解它是如何工作的吗?

最佳答案

只需将持续时间添加到当前时间:

auto now = boost::posix_time::microsec_clock::universal_time();

关于c++ - 创建 scoped_lock 最多 200 毫秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16711089/

相关文章:

c++ - 尝试解析 Boost Spirit 语法时出现编译时错误

c++ - 正则表达式在 g++ 4.9 下匹配但在 g++-5.3.1 下失败

c++ - auto* 在编译时有用还是 auto 关键字就足够了?

C++ 使用 boost 对字符串进行标记并将标记保存为字符串

c++ - 复制构造函数调用 boost::multi_index_container 中的自定义排序谓词

c++ - boost::date_time (boost-145) 使用带有微秒计算的 64 位 uint,没有截断

c++ - 删除 boost::interprocess::named_mutex 的问题

c++ - 我如何访问通过引用传递给 operator= 函数的对象的私有(private)数据?

c++ - 我可以多次访问 boost::interprocess::managed_shared_memory 而无需一次又一次地打开它吗?

c++ - boost 分配器无法在递归上下文中编译