Boost logo

Boost Users :

Subject: Re: [Boost-users] [Atomic] [Tread] Proposal for Synchronized<T>
From: Vicente J. Botet Escriba (vicente.botet_at_[hidden])
Date: 2013-02-20 13:25:20


Le 20/02/13 03:08, Gabriel Bizzotto a écrit :
>
> > My class allows in addition to lock several synchronized values using
> the deadlock free lock algorithm as e.g.
>
> I did not understand the unique_synchronize part, though.
>
synchronize() returns a strict_lock_ptr which is something like a
lock_guard that is not Lockable.
unique_synchronize() returns a unique_lock_ptr which is something like a
unique_lock (Lockable) and can be used with dead-lock free algorithms.
This facility can be used when locking several synchronized_value
instances to avoid dead-lock.

this dead-lock algorithm is used e.g. to define the assignment operation
as follows

     synchronized_value& operator=(synchronized_value const& rhs)
     {
       if(&rhs != this)
       {
         unique_lock<lockable_type> lk1(mtx_, defer_lock);
         unique_lock<lockable_type> lk2(rhs.mtx_, defer_lock);
         lock(lk1,lk2);

         value_ = rhs.value_;
       }
       return *this;
     }

so that

//thread1
a=b
// thread2
b=a

will not dead-lock.

Best,
Vicente



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net