title: If A ("whole") has a set of Bs ("parts"), how to synchronize the transfer of a "part" B from a "whole" A to another?
body: <p>Suppose we have whole-part object relationship. For instance, there is a <code>Boss</code> that can own multiple <code>Worker</code>s.
  For a <code>Boss</code>, we want the following operations:</p>
  
  <ul>
  <li>add a new <code>Worker</code> to its "collection"</li>
  <li>transfer an existing <code>Worker</code> object to another <code>Boss</code> object</li>
  </ul>
  
  <p>I provide some C++ code that illustrates this idea below (some functions were skipped).</p>
  
  <pre><code>class Worker {
  private:
      Boss *owner = nullptr;
      std::string name;
  
  public:
      Worker(std::string name) : name(name), owner(nullptr) { }
  
      Boss* getOwner() const {
          return this-&gt;owner;
      }
  
      void setOwner(Boss *owner) {
          this-&gt;owner = owner;
      }
  };
  
  class Boss {
  private:
      std::set&lt;Worker *&gt; workers;
      std::string name;
  
  public:
      Boss(std::string name) : name(name) { }
  
      ~Boss() {
          for (auto worker : this-&gt;workers) {
              delete worker;
          }
          this-&gt;workers.clear();
      }
  
      void addWorker(Worker *worker) {
          this-&gt;workers.emplace(worker);
          worker-&gt;setOwner(this);
      }
  
      void transferOwnership(Worker *dummyWorker, Boss *newOwner);
  };
  
  int main()
  {
      Boss b1{ "boss1" };
      Boss b2{ "boss2" };
  
      b1.addWorker(new Worker{ "w1" });
      b1.addWorker(new Worker{ "w2" });
  
      b2.addWorker(new Worker{ "w3" });
  
      Worker temp{ "w2" };
      b1.transferOwnership(&amp;temp, &amp;b2);
  
      b1.print(); // boss: boss1 having workers: w1
      b2.print(); // boss: boss2 having workers: w2 w3
  
      return 0;
  }
  </code></pre>
  
  <p>A <strong>serial</strong> implementation of the <code>transferOwnership()</code> function would be the following:</p>
  
  <pre><code>void Boss::transferOwnership(Worker *dummyWorker, Boss *newOwner) {
      // find existing worker
      Worker *actualWorker = nullptr;
      for (auto worker : this-&gt;workers) {
          if (*worker == *dummyWorker) {
              actualWorker = worker;
          }
      }
  
      if (nullptr == actualWorker) {
          return;
      }
  
      // remove existing worker from this
      this-&gt;workers.erase(actualWorker);
  
      // add worker to other (newOwner)
      newOwner-&gt;workers.emplace(actualWorker);
      actualWorker-&gt;setOwner(newOwner);
  }
  </code></pre>
  
  <p>Now, we want to parallelize this scenario. Suppose there are multiple threads that can execute any of the available operations (<code>addWorker()</code> or <code>transferOwnership()</code>) on any of the existing <code>Boss</code> objects.</p>
  
  <p>The question is how to synchronize the code such that we always get a consistent result at the end and not get into deadlocks.</p>
  
  <p>I think a solution would be to have a static mutex variable inside the <code>Boss</code> class, lock it at the beginning of the operation and end it at the end. This will protect all <code>workers</code> sets of all <code>Boss</code> objects.</p>
  
  <p>Another solution would be to have a mutex for each <code>Boss</code> object. However, I think here we might encounter deadlocks as in <code>transferOwnership()</code> we will need to lock/unlock the mutexes of both <code>Boss</code>es and I think we cannot impose a certain locking order in this situation.</p>
  
  <p>Can you think of other/better solutions?</p>