How can I create a smart pointer that locks and unlocks a mutex?

前端 未结 3 447
迷失自我
迷失自我 2021-01-01 19:51

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

3条回答
  •  有刺的猬
    2021-01-01 20:27

    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 another shared_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 a shared_ptr to a member value based on shared_ptr to object that it is a member of.

    Here we can "abuse" the form to provide lock guard. Do it like this:

    auto A::getResource()
    {
        auto counter = std::make_shared(&mMutex);
        std::shared_ptr result{ counter, &mResource };
        return result;
    }
    

    The returned shared_ptr points to mResource and keeps mMutex 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 locking mMutex 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 simple struct that keeps both the Lock and another shared_ptr to the A object owning the mResource.

提交回复
热议问题