Synchronization of set / get C ++ methods

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?

+4
2

, , R/W.

"READ" "WRITE" .

Boost shared_mutex - ,

boost shared_mutex ( / )?

"const",

+6

std::atomic .

[:] , . , , .

+3

Source: https://habr.com/ru/post/1606129/


All Articles