使用Boost ::与包含Boost :: Mutex的类绑定
Using boost::bind with a class containing a boost::mutex
我正在使用WatchDir在服务器上工作,将项目添加到内部集合中。WatchDir定期通过像这样创建的线程浏览:
this->watchDirThread = new boost::thread(boost::bind(&Filesystem::watchDirThreadLoop,
this,
this->watchDir,
fileFoundCallback));
fileFoundCallback
参数也是通过boost::bind
创建的:
boost::bind(&Collection::addFile, this->collection, _1)
我想保护我的集合免受使用MUTEX的并发访问,但我的问题是boost::mutex
类是不可复制的,因此我的Collection
类中不能有一个utex,因为boost::bind
需要可复制的参数。
我不喜欢静态静音的想法,因为它在语义上是错误的,因为这里的静音的角色是防止在修改时阅读我的收藏。
我该怎么办来解决这个问题?
使用std :: ref或std :: cref围绕互不rutex。也就是说,而不是:
boost::mutex yourmutex;
boost::bind(..., yourmutex, ...);
写:
boost::mutex yourmutex;
boost::bind(..., std::ref(yourmutex), ...);
相关文章:
- Boost 绑定和"result_type":不是成员,对 c++03 友好
- ROS 订阅回调 - 使用 boost::绑定成员函数
- 当Boost ::绑定模板函数时,模板参数扣除/替换失败
- 使用boost ::绑定结果作为参数
- boost ::绑定发送变量的回调args到回调函数
- 传递 boost::绑定参数作为参数
- boost::绑定在存储时不保存部分参数
- boost::绑定静态函数的参数
- 使用 boost :: 绑定将映射作为参数传递
- boost::与boost:asio绑定在一起.boost::绑定后续
- boost::与boost:asio绑定在一起.boost::绑定不起作用,从示例中复制
- Boost绑定和Boost函数,将带有参数的函数存储在向量中,然后执行它们
- boost::绑定具有默认值的成员函数
- boost::绑定到类成员函数
- boost::绑定重载函数
- boost绑定或返回常量的lambda函数
- boost::绑定内部副本
- boost::绑定模板化的函子
- 使用boost绑定成员函数
- boost::绑定动态参数