title: Safely synchronizing a COM thread
body: <p>I've done a lot with multithreading in the past, but I'm fairly new to COM.  Anyway, here's my issue:</p>
  
  <p>I create a worker thread, which registers as an STA, and creates a COM object.  Then the worker thread and the main thread try to communicate with each other.  Using <code>CoMarshalInterThreadInterfaceInStream</code> and <code>CoGetInterfaceAndReleaseStream</code>, I can get the threads to call methods on the COM objects in the other thread.</p>
  
  <p>Here's what the worker thread looks like:</p>
  
  <pre><code>void workerThread()
  {
    CoInitialize(NULL);
    MyLib::IFooPtr foo = ...; // create my COM object
  
    // Marshall it so the main thread can talk to it
    HRESULT hr = CoMarshalInterThreadInterfaceInStream(foo.GetIID(),
                                                       foo.GetInterfacePtr(),
                                                       &amp;m_stream);
    if (FAILED(hr)) {
      // handle failure
    }
  
    // begin message loop, to keep this STA alive
    MSG msg;
    BOOL bRet;
    while( (bRet = GetMessage( &amp;msg, NULL, 0, 0 )) != 0)
    { 
      if (bRet == -1)  break;
  
      DispatchMessage(&amp;msg); 
    }
  }
  </code></pre>
  
  <p>In the main thread:</p>
  
  <pre><code>// launch the thread
  m_worker = boost::thread (&amp;workerThread);
  
  // get the interface proxy
  MyLib::IFooPtr foo;
  LPVOID vp (NULL);
  HRESULT hr = CoGetInterfaceAndReleaseStream(m_stream, foo.GetIID(), &amp;vp);
  if (SUCCEEDED(hr)) foo.Attach(static_cast&lt;MyLib::IFoo*&gt;(vp));
  </code></pre>
  
  <p>This creates the object (which takes a while to initialize), and allows the main thread to talk to it, and everything is properly synchronized with the COM Apartment stuff.  As far as I can tell from reading msdn, this seems to be the right way to do things.  Now the main thread can use its proxy to call methods on my COM object, and the worker thread will receive those calls over the message queue, properly dispatching them.</p>
  
  <p>However, <strong>what about synchronizing these threads?</strong></p>
  
  <p>Obviously in this case I want the main thread to wait to call <code>CoGetInterfaceAndReleaseStream</code> until after the worker thread has created that stream via <code>CoMarshalInterThreadInterfaceInStream</code>.  But how can I safely do that?</p>
  
  <p>From <a href="http://msdn.microsoft.com/en-us/library/ms809971.aspx" rel="nofollow">MSDN</a>, I should be using something like <code>MsgWaitForMultipleObjects</code>, so I can wait for my_condition OR new_message_arrived, and then I can do something like:</p>
  
  <pre><code>// verbatim from msdn
  while (TRUE)
  {
     // wait for the event and for messages
     DWORD dwReturn = ::MsgWaitForMultipleObjects(1,
                       &amp;m_hDoneLoading, FALSE, INFINITE, QS_ALLINPUT);
  
     // this thread has been reawakened. Determine why
     // and handle appropriately.
     if (dwReturn == WAIT_OBJECT_0)
       // our event happened.
       break ;
     else if (dwReturn == WAIT_OBJECT_0 + 1)
     {
       // handle windows messages to maintain
       // client liveness
       MSG msg ;
       while(::PeekMessage(&amp;msg, NULL, 0, 0, PM_REMOVE))
         ::DispatchMessage(&amp;msg) ;
     }
  }
  </code></pre>
  
  <p>But how do I mix <code>boost::thread.join()</code> and <code>boost::condition.wait()</code> with <code>MsgWaitForMultipleObjects</code>?  Is that even possible, or do I have to do something else to avoid a race condition?</p>