How to use a std::lock_guard without violating const correctness?

Asked 3 years, 6 months ago
Viewed 6k times
 
12

In a subclass, I have a private std::mutex m field that I use in an implementation of a base class pure virtual method to return a value in a thread-safe way (the value can be updated by another thread):

int SubClass::get() const // implements ‘virtual int get() = 0 const‘ of the base class
{            
    std::lock_guard<std::mutex> lck(m); 
    return value;
}

The compiler is telling me that this violates const correctness by producing an error:

error: binding ‘const std::mutex‘ to reference of type ‘std::lock_guard::mutex_type& {aka std::mutex&}‘ discards qualifiers

Is there a way to make this compliant and use std::lock_guard in a const-correct way? Simply changing it to const std::lock_guard changes nothing. I don‘t really understand which part is problematic, then again I‘m quite new to C++ ...

 
  •  
    Is m a member of SubClass? – 1201ProgramAlarm Jan 7 ‘18 at 0:13
  • 2
    This question is marked as duplicate, but it is not. To get to the supposedly duplicated question the answer (use the mutable keyword) should be already known... – lornova Jan 9 ‘19 at 15:28
28
 

The this pointer is implicitly const (it‘s actually an rvalue of type const SubClass *), so any member you are accessing are const implicitly.

If you could modify member object in a const function, then what would be the point of const? :)

The lock guard wants to modify the mutex, to lock and unlock it, but because it is const, it cannot, that is the problem. It doesn‘t matter if the

lock guard is const or not, because that has nothing to do with the ability of the lock guard to modify the mutex or not.

To effectively solve this problem, there is a keyword you can use to mark member objects that can be modified in const qualified functions.

Now obviously, you shouldn‘t abuse this feature and mark every object of your class with it:

class SubClass {
/*...*/
private:
    mutable std::mutex m;
  //^^^^^^^
/*...*/
}; 
 
  • 4
    Just to nit-pick (hey, this is C++), this is not a const pointer. That is, the type is still const SubClass* - it‘s just its value is an rvalue,
    so you cannot assign to it. It‘s as if the compiler supplied a magic macro #define this (this_ + 0), and
    the function‘s hidden argument was named this_coliru.stacked-crooked.com/a/a92dbf35021f8758 – GManNickG Jan 7 ‘18 at 0:20 
 

Your Answer

 
 
 
 
 

Not the answer you‘re looking for? Browse other questions tagged    or ask your own question.

How to use a std::lock_guard without violating const correctness?

上一篇:HCNA Routing&Switching之OSPF度量值和基础配置命令总结


下一篇:Gram矩阵(pytorch)