title: Multi-threading: How to 'Lock' an Object by One or More Threads
body: <p>I have one 'main' thread that creates an array of objects of class <code>SlowData</code>. These objects are relatively slow to manipulate with. This is why, when I initially load them, they are filled with stub data.</p>
  
  <p>Immediately after each object in the array is loaded, I can display the array in the UI. Parallel with that, I initiate the computation over the array. The <code>SlowDataComputation</code> object is instructed to report results to the UI (which in turn will visualize the modification of a particular <code>SlowData</code> object).</p>
  
  <p>Here's how this would look:</p>
  
  <pre><code>void main()
  {
    std::vector&lt;SlowData&gt; aData;
    aData.reserve(1000000); // one million instances
  
    Fast_PopulateVector(aData);
  
    DataUi aUi;
  
    SlowDataComputation aCalc;
    aCalc.SetUiCallback(aUi);           // instruct the object to periodically
                                        // report computation results to the UI
  
    aCalc.Slow_ComputeInSeveralThreads(aData);
  
    aUi.VisualizeInMessageLoop(aData);  // message loop that exits upon user signal
  
    if ( aCalc.StillWorking() )
      aCalc.Stop();                     // terminate threads gradually
  
    // after VisualizeInMessageLoop returns, aData will then be destroyed
  }
  </code></pre>
  
  <p>I want <code>SlowDataComputation</code> to launch <em>several</em> threads, each of which would process a certain segment within the array:</p>
  
  <pre><code>class SlowDataComputation
  {
    std::vector&lt;SlowData&gt; * myData;
    DataUi                * myUI;
  
  public:
    void Slow_ComputeInSeveralThreads(std::vector&lt;SlowData&gt; &amp; theV)
    {
      myData = &amp;theV;
  
      size_t aSize = myData-&gt;size();
      size_t aNumThreads = 10;
      size_t aBlockSize = aSize / aNumThreads;
  
      for (size_ti = 0; i &lt; aNumThreads; i++)
      {
        ComputationThread aThr(myData);
        aThr.SetBoundary(i * aBlockSize, (i+1) * aBlockSize);
        aThr.SetUiCallback(myUI);
  
        aThr.Run();   // process a given segment in the array
      }
    }
  };
  </code></pre>
  
  <p>Finally, here's some pseudo-code for the UI:</p>
  
  <pre><code>class DataUi
  {
    std::vector&lt;SlowData&gt; * myData;
  
  public:
    void VisualizeInMessageLoop()
    {
      Message aMsg;
  
      while ( HasMessage(aMsg) )
        if (aMsg.Code == Message::MSG_PROGRAM_QUIT)
          SignalQuit();  // next time, HasMessage will return false
  
      // return from message loop
    }
  };
  </code></pre>
  
  <p><strong>The question is</strong>: how do I secure the destruction of <code>SlowData</code> instances so that if <em>any of the ten</em> computation threads is currently working with an object, destruction needs to wait until each of these computation threads completes (either because it finished processing or because the <code>SlowDataComputation::Stop</code> method was called)? What synchronization primitive is most suitable for this task?</p>
  
  <p>I managed to solve this problem with <em>one</em> computation thread. I used Critical Sections. But this solution doesn't scale well for more than one computation threads.</p>