Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ boost::互斥don';在模板中使用时不起作用_C++_Templates_Boost_Mutex - Fatal编程技术网

C++ boost::互斥don';在模板中使用时不起作用

C++ boost::互斥don';在模板中使用时不起作用,c++,templates,boost,mutex,C++,Templates,Boost,Mutex,我正在尝试编写一个类模板: template<typename ObjType> class SharedBuffer: private boost::noncopyable 但是当我创建锁时,我得到一个奇怪的编译错误: template<typename ObjType> inline void SharedBuffer<ObjType>::clear(void){ boost::mutex::scoped_lock lk(myMonitor);

我正在尝试编写一个类模板:

template<typename ObjType> class SharedBuffer: private boost::noncopyable
但是当我创建锁时,我得到一个奇怪的编译错误:

template<typename ObjType>
inline void SharedBuffer<ObjType>::clear(void){
  boost::mutex::scoped_lock lk(myMonitor);
  myBuffer.clear();
}
模板
内联void SharedBuffer::clear(void){
boost::mutex::作用域锁定lk(myMonitor);
myBuffer.clear();
}
错误9错误C2664:
'boost::unique_lock::unique_lock(boost::unique_lock&'):
无法从转换参数1
“const boost::mutex”到“boost::unique_lock&”
我不知道为什么会这样。我没有将我的监视器声明为常量。我使用的是VS2010和boost 1.4.9,问题在于:

无法将参数1从“constboost::mutex”转换为“boost::unique\u lock&”

解决办法是:

私有:mutableboost::mutex myMonitor


是的,作用域锁定被定义为typedef unique\u lock作用域锁定工作表单(但有GCC和boost 1\u 46)。是的,如果你需要锁定来读取对象,那么锁定需要是可变的。你能详细说明一下吗?在非常量方法中,互斥量突然变为常量的确切原因。并且是允许所有const方法改变互斥的唯一解决方案吗?@daramarak,我真的不知道。我同意这个代码的错误看起来很奇怪。可能真正的问题是访问常量对象的非常量方法。@inkooboo,听起来像是一个可能的假设。很高兴得到确认。。。
template<typename ObjType>
inline void SharedBuffer<ObjType>::clear(void){
  boost::mutex::scoped_lock lk(myMonitor);
  myBuffer.clear();
}
Error   9   error C2664: 
'boost::unique_lock<Mutex>::unique_lock(boost::unique_lock<Mutex> &)' :
 cannot convert parameter 1 from
 'const boost::mutex' to 'boost::unique_lock<Mutex> &'