Boost logo

Boost :

From: Alexander Terekhov (terekhov_at_[hidden])
Date: 2001-07-18 07:40:55


> class X {
> public:
> mutable boost::mutex mtx;
> int a;
> X() : a(0) { }
> X(const X& other) : a(0) { boost::mutex::lock lock(other.mtx); a =
other.a; }
> ~X() { }
> };

IMHO the use of "lock" is really confusing here.
i would suggest to adopt more common "locker" or "guard"..

 class X {
 public:
    mutable boost::mutex mtxLock; // mutex is exclusive lock which does not
support
                                  // transfer of lock ownership. binary
semaphore is
                                  // exclusive lock which does support
transfer of
                                  // lock ownership.
    int a;
    X() : a(0) { }
    X(const X& other) : a(0) { boost::mutex::locker locker(other.mtxLock);
a = other.a; }
    ~X() { }
 };

or

 class X {
 public:
    mutable boost::mutex mtxLock; // mutex is exclusive lock which does not
support
                                  // transfer of lock ownership. binary
semaphore is
                                  // exclusive lock which does support
transfer of
                                  // lock ownership.
    int a;
    X() : a(0) { }
    X(const X& other) : a(0) { boost::mutex::guard guard(other.mtxLock); a
= other.a; }
    ~X() { }
 };

--
 class Y {
 public:
    mutable boost::semaphore semLock;
    int a;
    Y() : a(0) { }
    Y(const X& other) : a(0) { boost::semaphore::guard
guard(other.semLock); a = other.a; }
    ~Y() { }
 };
regards,
alexander.

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