Let's look at 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);
};
An instance of this class could be obtained simultaneously from several different threads. And then:
All getMethods (getFancyInt, getNiceFloat, getPerfectString) should not block each other. They do not change the internal state of the object.
All setMethod (setIntSomething, setInternalState) should:
- block each other - to avoid an inconsistent state of the object,
- block all getMethods - to avoid returning partially modified data,
- blocked by all getMethods - to avoid returning partially modified data.
A simple lock_guard with mutex will satisfy all requirements except one - getMethod will block other getMethod methods.
What solution would be simple and clean in such a scenario?