Lets consider such a class in C++:
class CuteClass
{
public:
int getFancyInt() const;
float getNiceFloat() const;
string getPerfectString() const;
void setIntSomething(int something);
void setInternalState(State newState);
};
The instance of this class could be accessed concurrently from several different threads. And then:
All getMethods (getFancyInt, getNiceFloat, getPerfectString) shouldn't block each other. They doesn't change the internal state of the object.
All setMethod (setIntSomething, setInternalState) should:
- block each other - to avoid inconsistent state of object,
- block all getMethods - to avoid returning partially changed data,
- be blocked by all getMethods - to avoid returning partially changed data.
A simple lock_guard with mutex will met all requirements except one - getMethod would then block other getMethods.
What solution would be easy and clean in such scenario?