0

I want to know is this a good design if I need multithread safe class.

1) Design methods as a normal non thread safe class would have them, taking care to tag methods with const if they dont modify members. Ofc take this with a grain of salt, for example concurant vector cant have pop_back(), only try_pop() ...

2) Add mutable mutex to the class and lock it using lock_guard every at begining every method(taking care not to do double lock on mutex if it is nonrecursive)

Example:

size_t MyClass::getSize() const
{
  lock_guard<mutex> lg(mtx); //mtx is mutable
  return cont.size();

}
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277

0 Answers0