可变boost::mutex可以分离锁和等待函数
mutable boost::mutex is it possible to separate lock and wait functions?
所以我有像read
这样的函数,可以从多个线程同时调用。但我也有一个write
的函数需要锁定所有read
的函数。在哪里可以找到创建这种架构的例子?
我知道我们可以有:
mutable boost::mutex the_read_mutex;
mutable boost::mutex the_write_mutex;
:
void write()
{
// make all new readers wait and wait for all other currently running read threads();
}
void read()
{
// do not make all new readers wait, and wait for all currently running write thread()
}
那么怎么做呢?
可以使用
boost::shared_mutex m
Reader()
shared_lock lock(m)
Writer()
upgradeable_lock lck(m)
upgrade_to_unique_lock uniqueLock(lck);
了解更多关于Boost锁:Boost线程同步机制
要了解您正在处理的问题的类别:维基百科链接到Reader-WriterLock
了解更多关于POSIX读写锁的信息,它以非常简单的语法直接为您提供了读写锁:
相关文章:
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 如何让LLDB在成功时退出,在失败时等待
- C++:如何读取分离变量,然后读取向量
- 分离一个静态常量 std::thread?
- .h 和.cpp文件分离时出错,但仅使用 .h 文件时没有错误.我做错了什么?
- 如何在 c++ 中将数据与文件流分离
- 等待整个 omp 块完成,然后再调用第二个函数
- 提升 ASIO - io_service 不要等待连接到线程
- 如何在C++中实现带有packaged_task的异步等待循环?
- 虚假唤醒是否会解锁所有等待线程,甚至是不相关的线程?
- 如何等待窗口隐藏在Qt中?
- 为什么堆栈和堆在内存中分离得如此之多?
- 具有可分离内核的 2D 模糊卷积
- 如何在不等待检索的情况下获取C++中的内存位置?
- 等待被迷住了,没有回来
- 为什么即使调用了析构函数,C++11 中的分离线程也可以执行
- 等待 WaitForMultipleObjects 窗口中的事件数量可变
- 一个线程等待多个线程事件
- 如何"stop"正在等待条件变量的分离线程?
- 可变boost::mutex可以分离锁和等待函数