Assuming I have the following simplified class and you want to protect the resource from multi-threaded access. How can I include sth as a class lock, where each "entry point" to the public interface must first obtain a class lock before allowing the interface to be used?
class MyClass
{
public:
void A();
void B();
void C();
void D();
void E();
private:
SharedResource _res;
}
void MyClass::A()
{
B();
C();
D();
E();
}
void MyClass::B()
{
}
void MyClass::C()
{
}
void MyClass::D()
{
}
void MyClass::E()
{
}
I could do this by locking the class mutex in each of the methods, and then apply two versions of the BE methods:
void MyClass::A()
{
std::lock<std::mutex> lock(_mutex);
B_mtx();
C_mtx();
D_mtx();
E_mtx();
}
void MyClass::B()
{
std::lock<std::mutex> lock(_mutex);
B_mtx();
}
void MyClass::B_mtx()
{
}
, , , .
? getLock, move-assigment, ? , ?