There are two classes. Class A has a private member std::mutex m_
. Class B has an instance of class A
as its member.
The goal is to let class B to be able to use m_
(which is in class A).
I tried adding an accessor method in class A as below, but it gives error no matching function for call to 'std::unique_lock<std::mutex>::unique_lock(std::mutex)'
.
- Is above error because
std::mutex
is non-copyable? - What is the suggested way to expose the
std::mutex
in this case?
class A {
public:
// does not work
std::mutex getMutex() {
return m_;
}
private:
std::mutex m_;
}
class B {
A a;
void someMethod() {
...
std::unique_lock<std::mutex> lock(a.m_);
...
}
}