I'm working with a C++ multithreaded program that has to read and write some attributes within a class using boost shared_mutex. The program creates multiple object instances of a class A while it is executing. This class A has an static shared_mutex that is shared by all class A objects. If one of the attributes of that class needs to be written in one of the object instances, the particular object instance needs to get exclusive access to the shared mutex by upgrading it to a unique lock. However, before doing that the set method checks if the class instance owns the lock, which looks a bit odd to me.
This is more or less how class A hpp file looks like:
#include <boost/thread.hpp>
class A{
private:
//Private methods
void setAttribute(boost::upgrade_lock<boost::shared_mutex>& lock, const bool value);
//Private variables
static boost::shared_mutex mainMutex;
mutable bool attribute;
public:
//... Some public methods using setAttribute
}
And this is how class A cpp file looks like:
void A::setAttribute(boost::upgrade_lock<boost::shared_mutex>& lock, const bool value)
{
(void)lock;
assert(lock.owns_lock());
//get exclusive access to the lock
const boost::upgrade_to_unique_lock<boost::shared_mutex> ulock(lock);
attribute = value;
}
So, my question is why would I need to check if the lock passed owns the lock given that the method setAttribute is private and it's only used by methods within the class. Are there any circumstances where I should consider doing this?