title: Why can't I interrupt this particular boost::thread?
body: <p>I have two tests for interrupting a boost::thread. One works, the other doesn't. Can anyone tell me why?</p>
  
  <p><strong>Working:</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;boost/thread.hpp&gt;
  #include &lt;boost/chrono.hpp&gt;
  #include &lt;unistd.h&gt;
  
  using namespace std;
  
  void Run(void)
  {
      try
      {
          cout &lt;&lt; "Run()\n";
          for ( int i = 0; i &lt; 1000; i++ )
          {
              cout &lt;&lt; "Thread: " &lt;&lt; i &lt;&lt; endl;
              boost::this_thread::sleep(boost::posix_time::milliseconds(500));
          }
      } catch (...)
      {
          cout &lt;&lt; "INTERRUPTED!\n";
      }
      cout &lt;&lt; "Thread returning.\n";
  };
  
  int main()
  {
      boost::thread my_thread(Run);
      sleep(1);
      cout &lt;&lt; "Main() sleeping\n";
      sleep(1);
      cout &lt;&lt; "Main() interrupting the thread\n";
      my_thread.interrupt();
      sleep(1);
      cout &lt;&lt; "Main() bye!!\n";
  }
  </code></pre>
  
  <p>Compile like so: <code>g++ test1.cpp -lboost_thread -lboost_system; ./a.out</code></p>
  
  <p>Output is:</p>
  
  <pre><code>Run()
  Thread: 0
  Thread: 1
  Main() sleeping
  Thread: 2
  Thread: 3
  Main() interrupting the thread
  INTERRUPTED!
  Thread returning.
  Main() bye!!
  </code></pre>
  
  <p><strong>Broken:</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;boost/thread.hpp&gt;
  #include &lt;boost/chrono.hpp&gt;
  #include &lt;unistd.h&gt;
  
  using namespace std;
  
  class CThread
  {
  public:
      void Interrupt() { cout &lt;&lt; "calling interrupt\n"; ThreadHandle.interrupt(); }
  
  protected:
      static unsigned int Init(void * process);
      virtual int Run(void) =0;
      void StartThread(void);
      boost::thread ThreadHandle;
  };
  
  unsigned int CThread::Init(void * process)
  {
      cout &lt;&lt; "Init()\n";
      return ((CThread *)process)-&gt;Run();
  }
  
  void CThread::StartThread(void)
  {
      boost::thread ThreadHandle(CThread::Init, this);
  }
  
  class my_thread_class : public CThread
  {
      public:
      my_thread_class();
  
      int Run(void)
      {
         cout &lt;&lt; "Run(), thread running\n";
          for ( int i = 0; i &lt; 1000; i++ )
          {
              cout &lt;&lt; "Thread: " &lt;&lt; i &lt;&lt; endl;
              boost::this_thread::sleep(boost::posix_time::milliseconds(200));
          }
          cout &lt;&lt; "Thread returning.\n";
          return 0;
      };
  };
  
  my_thread_class::my_thread_class()
  {
      StartThread();
  }
  
  int main()
  {
      my_thread_class my_thread;
      sleep(1);
      cout &lt;&lt; "Main() sleeping\n";
      sleep(2);
      cout &lt;&lt; "Main() interrupting the thread\n";
      my_thread.Interrupt();
      sleep(5);
      cout &lt;&lt; "Main() bye!!\n";
  }
  </code></pre>
  
  <p>Same compilation, and the broken output is:</p>
  
  <pre><code>Init()
  Run(), thread running
  Thread: 0
  Thread: 1
  Main() sleeping
  Thread: 2
  Thread: 3
  Main() interrupting the thread
  calling interrupt
  Thread: 4
  Thread: 5
  Main() bye!!
  </code></pre>
  
  <p>So it doesn't appear to interrupt in my broken case. </p>