title: How can I propagate exceptions between threads?
body: <p>We have a function which a single thread calls into (we name this the main thread). Within the body of the function we spawn multiple worker threads to do CPU intensive work, wait for all threads to finish, then return the result on the main thread.</p>
  
  <p>The result is that the caller can use the function naively, and internally it'll make use of multiple cores.</p>
  
  <p><em>All good so far..</em></p>
  
  <p>The problem we have is dealing with exceptions.  We don't want exceptions on the worker threads to crash the application.  We want the caller to the function to be able to catch them on the main thread.  We must catch exceptions on the worker threads and propagate them across to the main thread to have them continue unwinding from there.</p>
  
  <p><strong><em>How can we do this?</em></strong></p>
  
  <p>The best I can think of is:</p>
  
  <ol>
  <li>Catch a whole variety of exceptions on our worker threads (std::exception and a few of our own ones).</li>
  <li>Record the type and message of the exception.</li>
  <li>Have a corresponding switch statement on the main thread which rethrows exceptions of whatever type was recorded on the worker thread.</li>
  </ol>
  
  <p>This has the obvious disadvantage of only supporting a limited set of exception types, and would need modification whenever new exception types were added.</p>