2012-12-21 57 views
0

我正在使用watchdir将服务器添加到内部集合的服务器上工作。将boost :: bind与包含boost :: mutex的类一起使用

this->watchDirThread = new boost::thread(boost::bind(&Filesystem::watchDirThreadLoop, 
                 this, 
                 this->watchDir, 
                 fileFoundCallback)); 

fileFoundCallback参数也通过boost::bind创建:该wa​​tchdir由被这样创建一个线程定期浏览

boost::bind(&Collection::addFile, this->collection, _1) 

我想用互斥来保护我的收藏从并发访问但我的问题是boost::mutex类是不可复制的,因此Collection类中不能有互斥体,因为boost::bind需要可复制参数。

我不喜欢静态互斥体的想法,因为它在语义上是错误的,因为互斥体的作用是阻止我的集合在被修改时被读取。

我能做些什么来解决这个问题?

回答

3

在互斥体周围使用std::ref or std::cref。也就是说,不是,:

boost::mutex yourmutex; 
boost::bind(..., yourmutex, ...); 

写:

boost::mutex yourmutex; 
boost::bind(..., std::ref(yourmutex), ...); 
相关问题