2607
2607

Reputation: 4115

Mutex class member

If I have a class with 5 member variables, e.g.,

class XYZ
{
public:
double x1;
......
double x5;
};

There are 5 threads which each one of them access 1 member variable of the same object (which one is unknown). If I want to make it thread-safe and also efficient, is it necessary to create 5 mutex member in the class as well. In other words,

class XYZ
{
public:
double x1;
......
double x5;
boost::mutex mutex1;
......
boost::mutex mutex5;
};

Thanks.

Upvotes: 1

Views: 1399

Answers (1)

Alok Save
Alok Save

Reputation: 206508

A mutex can be locked and unlocked from the same thread, so if you have 5 different threads which will acquire 5 different critical sections at the same time, Yes you will need separate mutex for each of them.

Upvotes: 2

Related Questions