Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2005-02-14 14:22:42


Raz wrote:
> Why doesn't Boost have any smart pointers that automatically lock a
> class when a member is used? The code isn't that hard.
>
> #include <iostream>
>
> struct Dummy { int a; }
>
> class Lock {
> private:
> Dummy &_d;
> public:
> Lock( Dummy &d ) : _d(d) { std::cout << "Locked." << std::endl; }
> ~Lock( void ) { std::cout << "Unlocked." << std::endl; }
>
> Dummy *operator->( void ) { return &_d; }
> };
>
> class Ref {
> private:
> Dummy &_d;
> public:
> Ref( Dummy &d ) : _d(d) { }
>
> Lock operator->(void) { return Lock(_d); }
> };

Your code is non-portable. You are assuming that the temporary Lock(_d) will
be constructed directly in the return value, but this is not guaranteed.

That aside, I don't know why Boost doesn't include a locking pointer.
Probably because locking at pointer level is rarely the best solution when
access to a class needs to be synchronized.


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