Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2002-05-17 05:16:38


From: "David B. Held" <dheld_at_[hidden]>
>
> This doesn't have anything to do with threads, but it's interesting that
> you found another possible use for move semantics. I'm not sure if
> you could create the lock you need out of a policy-based smart pointer,

[...]

Viewing a lock as a limited smart pointer, where the resource is acquired
via mutex::do_lock and released via mutex::do_unlock is a powerful idea. Now
one can unleash the "smart pointer theory" on the problem. ;-)

A quick-and-dirty prototype along these lines:

class mutex
{
    mutex * do_lock()
    {
        // lock
        return this;
    }

    void do_unlock();

public:

    class shared_lock;
    friend class shared_lock;

    class shared_lock
    {
        shared_ptr<mutex> sp_;

    public:

        explicit shared_lock(mutex & m): sp_(m.do_lock(),
mem_fn(&mutex::do_unlock))
        {
        }
    }
};

An interesting topic for research that may produce an article or even a
series. It isn't a move semantics showcase IMHO but it's a good
pro-smart_ptr example (compared to shared_ptr) and has an interesting
chicken-and-egg subtlety w.r.t. the thread safety of the reference count.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk