Search code examples
boostlockingshared-ptrboost-thread

'boost shared_ptr' and 'boost lock' together = messed up


I am new to both concepts shared_ptr and mutex (boost or not boost). I am trying to use it in my classes :

typedef boost::shared_mutex Lock;
typedef boost::unique_lock< Lock > WriteLock;
typedef boost::shared_lock< Lock > ReadLock;

class subscriptionInfo 
{
public:
//this is not a copy constructible class. so I have to use shared pointer
boost::shared_ptr<Lock> myLock; 
...
}
...
std::vector<DATA_MSG_PTR>& subscriptionInfo::getIncoming() {
    ReadLock Lock(myLock);
    return incoming;
}

and the error says:

error: no matching function for call to ‘boost::shared_lock<boost::shared_mutex>::shared_lock(boost::shared_ptr<boost::shared_mutex>&)’

I will appreciate if you help me find out what I messed up and how to solve it. thanks


Solution

  • The myLock member is a pointer. A smart one but in any case a pointer. The shared_lock constructor accepts a reference to a mutex object and not a pointer. That is why the error message takes place. To solve the problem you have to dereference the pointer as ReadLock Lock(*myLock);