   Name of this synchronization mechanism & how to do it
  HiI have been trying to implement this for days but couldn't come up with perfect solution. Here's description on what I need.- There are 2 kinds of objects, say, "class Master" and "class Attached".- Each "Attached" object contains pointer to "Master" object.- There are 5 operations : "lock master", "unlock master", "lock attached", "unlock attached", and "move"The pseudocode for this :class Master{public:void lock();  // "lock master" operationvoid unlock();// "unlock master" operation};class Attached{public:Master* getMaster();void lock();  // "lock attached" operationvoid unlock();// "unlock attached" operationvoid moveTo(Master*);   // "move" operation};And here is description on how they should work :- Each call to Master::lock() shall be accompanied by Master::unlock(). The same for Attached::lock() and Attached::unlock().- A successful completion of m->lock() (where Master* m) should block    * subsequent calls to m->lock()    * subsequent calls to a->lock() (where Attached* a) if a->getMaster() == m  until next time m->unlock() is called.- A successful completion of a->lock() (where Attached* a) should block    * subsequent calls to a->lock()    * subsequent calls to a->getMaster()->lock()    * subsequent calls to a->moveTo(...)  until next time a->unlock() is called.- After successful completion of a->moveTo(m) (where Attached* a, Master* m), a->getMaster() should return m.I tried to explain the problem as precisely as I could. If it's confusing or not readable, here is another explanation:- Master::lock() and Master::unlock() locks/unlocks the object.- Attached::lock() and Attached::unlock() locks/unlocks both the object itself and its current master.- Attached::moveTo() changes its master.I could came up with a few ways but each of them had either potential deadlock or race conditions. Would you tell me  1) what on earth this synchronization mechanism is called  2) and hopefully, some tips & hints on how to do it (a full solution would be much appreciated :)Thank you so much   
  you just basically badly described whats called a "gate" lock.http://blog.affien.com/archives/2006/03/02/locks/   ;  It looks like sharing a single mutex between the Master object and the Attached object should do the trick, no?