2

Is it possible to use mutexes on parts of a class ?

Like :

class A{
    int a;
    int b;
    boost::mutex Mutex_for_a;
    boost::mutex Mutex_for_b;
}

and then use the right mutex to perform the right operation. I know this is technically possible but I don't know if it can lead to problems.

Aimery
  • 1,559
  • 1
  • 19
  • 24
  • And what problems are you thinking of? – Hatted Rooster May 15 '18 at 10:30
  • 2
    https://stackoverflow.com/questions/24225201/classes-and-mutex – Sumit Jha May 15 '18 at 10:32
  • I have a big class with several vectors, enums and more. They are used by several threads. Every function only need to access 2 or 3 variables of the 13 variables so I want to use several mutexes. I does not work for now since some functions need almost permanent access to a vector (can't make a copy, the vector is changing). – Indetronable May 15 '18 at 10:43

3 Answers3

4

I know this is technically possible but I don't know if it can lead to problems.

This is certainly possible. However, if not handled carefully, things like this can lead to deadlocks:

A::use_a() { std::lock_guard lck{ Mutex_for_a }; use_b(); ... }
A::use_b() { std::lock_guard lck{ Mutex_for_b }; use_a(); ... }

So you have to make sure that if your class design allows to have both mutexes locked at the same time, the locking order is consistent everwhere (or better use std::scoped_lock).

Jodocus
  • 7,493
  • 1
  • 29
  • 45
  • Beside deadlocks, there is no problem to have one mutex per cluster of variables inside an instance of a class ? – Indetronable May 15 '18 at 10:49
  • @Indetronable There comes none to my mind which is generally applicable. I've done this myself as well quite some times and the mutex ordering made most of the trouble in the past (where there was no C++11 yet). – Jodocus May 15 '18 at 10:58
  • I am not coding in c++11. I am coding with a kind of c++10 (have access ti decltype auto but not updated libs). – Indetronable May 15 '18 at 11:02
  • @Indetronable That's not very advisory, but Boost should certainly have equivalent facilities. – Jodocus May 15 '18 at 11:03
1

Is it possible to use mutex on parts of a class ?

You don't use a mutex on anything. A mutex is a thing that your threads can "lock" and "unlock," and it won't let more than one thread lock it at the same time. That's all. A mutex does not know why your threads lock it. A mutex does not know or care which objects or data your code associates with it.

class A{ int a; int b; boost::mutex Mutex_for_a; boost::mutex Mutex_for_b; }

That might sense, or it might not. There's no way to tell without seeing how your threads use a and b. The main reason for using a mutex is to prevent other threads from seeing some collection of data in an inconsistent or invalid state while some other thread is in the middle of changing it.

If the "collection of data" that you want to protect is contained within a single int variable, it might make more sense to share it by making it a std::atomic<int>, and forget about the mutex.

On the other hand, if there is some important relationship between a and b, then you should be using a single mutex to protect that relationship.

Solomon Slow
  • 25,130
  • 5
  • 37
  • 57
0

Yes, it is possible. Only catch is that it will be object specific. You should not be locking a mutext and waiting for another, which can lead to deadlock situation.

Jay KM
  • 21
  • 2