title: How do I catch a boost::thread_interrupted exception in a worker thread?
body: <p>I am having trouble catching an interrupt in a worker thread. There are numerous boost thread interrupt posts on here but, they seem to be silly mistakes (which I'm sure my question is too), or something that didn't help</p>
  <ul>
  <li><a href="https://stackoverflow.com/questions/3189306/boost-thread-interrupted-exception-terminates-with-mingw-gcc-4-4-0-ok-with-3">Boost thread_interrupted exception terminate()s with MinGW gcc 4.4.0, OK with 3.4.5</a></li>
  <li><a href="https://stackoverflow.com/questions/19039675/boost-thread-on-interruption-doesnt-print-exit-message">boost thread on interruption doesn&#39;t print exit message</a></li>
  <li>this example crashes <a href="https://stackoverflow.com/questions/4102811/where-to-get-simple-boost-thread-management-example/4103005#4103005">Where to get simple Boost Thread Management example?</a></li>
  <li>My example is based off this, it still crashes <a href="https://stackoverflow.com/questions/12437395/a-simple-example-of-boost-multithreading/12437526#12437526">How can I execute two threads asynchronously using boost?</a></li>
  </ul>
  <p>In my example have a main thread that creates a worker thread. The main thread waits for the user to press enter while the worker thread is in a while loop, and sleeps for 1 second intervals. When the user presses enter the main thread interrupts the worker thread and then calls join, the worker thread should throw a thread_interrupted exception at  boost::this_thread::sleep_for(sec) which should be caught and then the worker function should exit. This allows the main thread to continue and then exits the program.</p>
  <pre><code>#define BOOST_THREAD_USE_LIB 1
  #define BOOST_SYSTEM_NO_DEPRECATED 1
  #define _WIN32_WINNT 0x0501
  #define WINVER 0x0501
  
  #include &lt;boost/thread.hpp&gt;
  #include &lt;boost/chrono/include.hpp&gt;
  #include &lt;boost/chrono/duration.hpp&gt;
  #include &lt;iostream&gt;
  
  
  using namespace std;
  
  void ThreadFunctionSleep()
  {
      cout &lt;&lt;  boost::this_thread::get_id() &lt;&lt; &quot; worker thread &quot; &lt;&lt; endl;
      int counter = 0;
  
      while(1)
      {
          cout &lt;&lt;  boost::this_thread::get_id() &lt;&lt; &quot; thread iteration &quot; &lt;&lt; ++counter &lt;&lt; &quot; Press Enter to stop&quot; &lt;&lt; endl;
  
          try
          {
              boost::chrono::seconds sec(1);
              boost::this_thread::sleep_for(sec);
          }
          catch(boost::thread_interrupted&amp;)
          {
              cout &lt;&lt;  boost::this_thread::get_id() &lt;&lt; &quot; Thread is stopped in ThreadFunction &quot; &lt;&lt; endl;
              return;
          }
          catch ( boost::thread_resource_error &amp;)
          {
              cout &lt;&lt;  boost::this_thread::get_id() &lt;&lt; &quot; boost thread_resource_error&quot; &lt;&lt; endl;
              return;
          }
          catch(...)
          {
              cout &lt;&lt;  boost::this_thread::get_id() &lt;&lt; &quot; worker func other&quot; &lt;&lt; endl;
              return;
          }
  
      }
  
  
  
  
  }
  
  int main()
  {
      std::cout &lt;&lt;  boost::this_thread::get_id() &lt;&lt; &quot; main thread &quot; &lt;&lt; std::endl;
  
      // Start thread
      boost::thread t(&amp;ThreadFunctionSleep);
  
  
      // Wait for Enter
      std::cout &lt;&lt;  boost::this_thread::get_id() &lt;&lt; &quot; Waiting for Enter&quot; &lt;&lt; std::endl;
      char ch;
      cin.get(ch);
  
      // Ask thread to stop
      //std::cout &lt;&lt;  t.get_id() &lt;&lt; &quot; joinable &quot; &lt;&lt; t.joinable() &lt;&lt; std::endl;
      try
      {
          t.interrupt();
      }
      catch(boost::thread_interrupted&amp;)
      {
          std::cout &lt;&lt;  boost::this_thread::get_id() &lt;&lt; &quot; Thread is stopped&quot; &lt;&lt; std::endl;
  
      }
      catch ( boost::thread_resource_error &amp;)
      {
          std::cout &lt;&lt;  boost::this_thread::get_id() &lt;&lt; &quot; boost thread_resource_error&quot; &lt;&lt; std::endl;
  
      }
  
  
  
      // Join - wait when thread actually exits
      try
      {
          t.join();
  
      }
      catch(boost::thread_interrupted&amp;)
      {
          std::cout &lt;&lt;  boost::this_thread::get_id() &lt;&lt; &quot; Thread is joined&quot; &lt;&lt; std::endl;
  
      }
      catch(boost::system::system_error &amp;)
      {
          std::cout &lt;&lt;  boost::this_thread::get_id() &lt;&lt; &quot; boost system_error&quot; &lt;&lt; std::endl;
  
      }
      catch(...)
      {
          std::cout &lt;&lt;  boost::this_thread::get_id() &lt;&lt; &quot; other exception&quot; &lt;&lt; std::endl;
  
      }
  
      cout &lt;&lt;  boost::this_thread::get_id() &lt;&lt; &quot; main: thread ended&quot; &lt;&lt; endl;
  
  
  
  
      return 0;
  }
  </code></pre>
  <p>The output of my program I expect is something like this:</p>
  <pre><code>4f4 main thread
  1264 worker thread
  4f4 Waiting for Enter
  1264 thread iteration 1 Press Enter to stop
  1264 thread iteration 2 Press Enter to stop
  1264 Thread is stopped in ThreadFunction 
  4f4 main: thread ended
  </code></pre>
  <p>Where the numbers in front is the thread ID. The problem is, I am not seeing this at all. There are 3 different ways the code executes after I press Enter. This suggests some sort of multithreaded problem that I'm not seeing.</p>
  <ol>
  <li><p>No exception is caught until t.join(), the worker thread continues the executuion of the main thread</p>
  <p>11cc main thread
  11cc644 worker thread
  644 thread iteration 1 Press Enter to stop
  Waiting for Enter
  644 thread iteration 2 Press Enter to stop</p>
  <p>644 Thread is joined
  644 main: thread ended</p>
  <p>Process returned 0 (0x0)   execution time : 2.750 s
  Press any key to continue.</p>
  </li>
  <li><p>Program crashes</p>
  <p>11cc main thread
  11cc644 worker thread
  644 thread iteration 1 Press Enter to stop
  Waiting for Enter
  644 thread iteration 2 Press Enter to stop</p>
  <p>644 Thread is joined
  644 main: thread ended</p>
  <p>Process returned 0 (0x0)   execution time : 2.750 s
  Press any key to continue.</p>
  </li>
  <li><p>happens very rarely, but crashes anyway</p>
  <p>11cc main thread
  11cc644 worker thread
  644 thread iteration 1 Press Enter to stop
  Waiting for Enter
  644 thread iteration 2 Press Enter to stop</p>
  <p>644 Thread is stopped in ThreadFunction</p>
  </li>
  </ol>
  <p>How do I catch boost::thread_interrupted in the worker thread so that the main thread will continue as normal?? On a similar note, will I need to consider anything else if I use a timed_wait() instead of sleep_for()??</p>
  <p>I am using boost 1.53.0 and MinGW 4.4.1. I am using the runtime-link-static, multithreading libraries for boost::thread and boost::system</p>
  <p>Thanks</p>