I have a threaded class from which I would like to occasionally acquire a pointer an instance variable. I would like this access to be guarded by a mutex so that the thread is blocked from accessing this resource until the client is finished with its pointer.
My initial approach to this is to return a pair of objects: one a pointer to the resource and one a shared_ptr to a lock object on the mutex. This shared_ptr holds the only reference to the lock object so the mutex should be unlocked when it goes out of scope. Something like this:
void A::getResource()
{
Lock* lock = new Lock(&mMutex);
return pair<Resource*, shared_ptr<Lock> >(
&mResource,
shared_ptr<Lock>(lock));
}
This solution is less than ideal because it requires the client to hold onto the entire pair of objects. Behaviour like this breaks the thread safety:
Resource* r = a.getResource().first;
In addition, my own implementation of this is deadlocking and I'm having difficulty determining why, so there may be other things wrong with it.
What I would like to have is a shared_ptr that contains the lock as an instance variable, binding it with the means to access the resource. This seems like something that should have an established design pattern but having done some research I'm surprised to find it quite hard to come across.
My questions are:
- Is there a common implementation of this pattern?
- Are there issues with putting a mutex inside a shared_ptr that I'm overlooking that prevent this pattern from being widespread?
- Is there a good reason not to implement my own shared_ptr class to implement this pattern?
(NB I'm working on a codebase that uses Qt but unfortunately cannot use boost in this case. However, answers involving boost are still of general interest.)
There is another approach here. Far less flexible and less generic, but also far simpler. While it still seems to fit your exact scenario.
shared_ptr
(both standard and Boost) offers means to construct it while providing anothershared_ptr
instance which will be used for usage counter and some arbitrary pointer that will not be managed at all. On cppreference.com it is the 8th form (the aliasing constructor).Now, normally, this form is used for conversions - like providing a
shared_ptr
to base class object from derived class object. They share ownership and usage counter but (in general) have two different pointer values of different types. This form is also used to provide ashared_ptr
to a member value based onshared_ptr
to object that it is a member of.Here we can "abuse" the form to provide lock guard. Do it like this:
The returned
shared_ptr
points tomResource
and keepsmMutex
locked for as long as it is used by anyone.The problem with this solution is that it is now your responsibility to ensure that the
mResource
remains valid (in particular - it doesn't get destroyed) for that long as well. If lockingmMutex
is enough for that, then you are fine.Otherwise, above solution must be adjusted to your particular needs. For example, you might want to have the
counter
a simplestruct
that keeps both theLock
and anothershared_ptr
to theA
object owning themResource
.You're describing a variation of the EXECUTE AROUND POINTER pattern, described by Kevlin Henney in Executing Around Sequences.
I have a prototype implementation at
exec_around.h
but I can't guarantee it works correctly in all cases as it's a work in progress. It includes a functionmutex_around
which creates an object and wraps it in a smart pointer that locks and unlocks a mutex when accessed.I'm not sure if there are any standard implementations, but since I like re-implementing stuff for no reason, here's a version that should work (assuming you don't want to be able to copy such pointers):