
20 Jul
2007
20 Jul
'07
11:22 p.m.
On 7/20/07, Peter Dimov <pdimov@pdimov.com> wrote:
The classic solution is to lock the mutexes in ascending order of their addresses, but I prefer
X& X::operator=( X const& rhs ) { X tmp( rhs ); // temporarily locks rhs.mutex
scoped_lock lock( this->mutex ); swap( tmp ); // or move_from( tmp )
return *this; }
It's always a good idea to only lock one mutex at a time if you can afford it.
Very nice! Thanks Michael and Peter. I especially liked your last solution. I guess that shows how an efficient swap function can really come in handy. Phillip