[英]Using boost::mutex::scoped_lock inside const function
This code won't compile: 此代码将无法编译:
class MyClass
{
boost::mutex _mutex;
void foo() const
{
boost::mutex::scoped_lock lock(_mutex);
//critical section
}
}
But defining the function as non const will work fine. 但是将函数定义为非const将正常工作。 Please, can someone explain why?
请问有人可以解释原因吗? Thanks!
谢谢!
You can't lock a mutex inside a const-member function because this actually modifies the internal state of the mutex ( lock
is not itself a const
function). 你不能在const-member函数中锁定互斥锁,因为它实际上修改了互斥
lock
的内部状态( lock
本身不是const
函数)。
If you want to keep the function const
, you'll have to declare the mutex as mutable
which is a cv-qualifier that allows const functions to modify it, ie 如果你想保持函数
const
,你必须将互斥量声明为mutable
,这是一个允许const函数修改它的cv限定符,即
//can now be locked (i.e. modified) by a const function
mutable boost::mutex _mutex;
Using mutable
relax the const
constraints on the member variable which is declared with this qualifier, that's a way to get around constness, so be careful not to abuse this. 使用
mutable
放松对使用此限定符声明的成员变量的const
约束,这是一种绕过constness的方法,所以小心不要滥用它。 In this case, it seems reasonable because a mutex is an internal tool of your class, and does not participate in "logical constness" (as opposed to "bitwise constness"). 在这种情况下,它似乎是合理的,因为互斥体是类的内部工具,并且不参与“逻辑常量”(与“按位常量”相反)。
This code should compile 这段代码应该编译
class MyClass
{
mutable boost::mutex _mutex;
void foo() const
{
boost::mutex::scoped_lock lock(_mutex);
//critical section
}
}
Raistmaj is right. Raistmaj是对的。
The reason is that a constant method guarantees it does not change its class instance. 原因是常量方法保证它不会更改其类实例。 By declaring the mutex mutable, you make an exception for that variable.
通过声明互斥锁可变,您为该变量设置了一个例外。
The problem occurs because boost::mutex::lock()
, which is called by the constructor of boost::mutex::scoped_lock
, is not a const
member function. 出现此问题的原因是
boost::mutex::scoped_lock
的构造函数调用的boost::mutex::lock()
不是 const
成员函数。 Since the mutex is a member of MyClass
, that means that MyClass::_mutex::lock()
cannot be called from a non- const
member function of MyClass
. 由于互斥锁是
MyClass
的成员,这意味着无法从MyClass
的非const
成员函数调用MyClass::_mutex::lock()
。
The solution is to declare the mutex as a mutable
member. 解决方案是将互斥锁声明为
mutable
成员。 This indicates to the compiler that _mutex
may be modified, even in a const
member function: 这向编译器表明即使在
const
成员函数中也可以修改_mutex
:
class MyClass
{
mutable boost::mutex _mutex;
void foo() const
{
boost::mutex::scoped_lock lock(_mutex);
//critical section
}
}
Please can you explain why it didn't work.
请你能解释为什么它不起作用。 Is locking the _mutex is 'modifing' it?
锁定_mutex是'修改'吗?
Precisely, the _mutex object will change it's internal state from say "unlocked" to "locked" state. 确切地说,_mutex对象会将其内部状态从“解锁”状态更改为“已锁定”状态。 So, you need the mutable keyword fora const-function to preserve logical constness of the function while allowing the mutex to be modifiable.
因此,您需要mutable关键字fora const-function来保留函数的逻辑const,同时允许互斥体可修改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.