title: Boost threads - safe/guaranteed way to handle thread interrupts
body: <p>I am moving a C program using <code>pthreads</code> over to C++ and will have to make extensive use of the Boost library for the sake of making the program multi-platform, portable, etc.</p>
  <p>When working with a thread in the past, my code will usually have the form:</p>
  <hr />
  <pre><code>void threadFunc(void* pUserData)
  {
      if ( !pUserData )
      {
          return;
      }
  
      myStruct* pData = (myStruct*)pUserData;
      bool bRun;
  
      lock(pUserData);
      bRun = pUserData-&gt;bRun;
      unlock(pUserData);
  
      while ( bRun )
      {
          // Do some stuff
          // ...
          // ...
          lock(pUserData);
          bRun = pUserData-&gt;bRun;
          unlock(pUserData);
      }
  
      /* Done execution of thread; External thread must have set pUserData-&gt;bRun
       * to FALSE.
       */
  }
  </code></pre>
  <hr />
  <p>This works as I would expect. When I want the thread to shut down, I just lock the mutex for the <code>struct</code> it's accessing (typically a member of the <code>struct</code>), toggle the <code>bRun</code> flag, and <code>join()</code> on the thread. For boost threads, I've noticed cases with my code where a <code>timed_join()</code> times out when doing non-blocking operations, <a href="https://stackoverflow.com/questions/7316633/boost-thread-how-to-acknowledge-interrupt">similar to this SO question</a>. This leads me to suspect I'm not using boost threads correctly.</p>
  <h1>To the question...</h1>
  <p><strong>First,</strong> which of the two general thread structures would be correct for having a thread properly catch the <code>thread_interrupted</code> exception?</p>
  <h1>Case A</h1>
  <hr />
  <pre><code>void threadFunc( void* pUserData )
  {
      while ( 1 )
      {
          try
          {
              // Do some stuff
              // ...
              // ...
              boost::this_thread::sleep(boost::posix_time::milliseconds(1));
          }
          catch(boost::thread_interrupted const&amp; )
          {
              // Thread interrupted; Clean up
          }
      }
  }
  </code></pre>
  <hr />
  <h1>Case B</h1>
  <hr />
  <pre><code>void threadFunc( void* pUserData )
  {
      try
      {
          while ( 1 )
          {
              // Do some stuff
              // ...
              // ...
              boost::this_thread::sleep(boost::posix_time::milliseconds(1));
          }
      }
      catch(boost::thread_interrupted const&amp; )
      {
          // Thread interrupted; Clean up
      }
  }
  </code></pre>
  <hr />
  <p><strong>Second,</strong> what would be the appropriate boost function to call in place of sleep if I want the thread to have a <strong>chance</strong> to catch the interrupt call, but not to <code>sleep()</code> or give up the remainder of the CPU time slice it currently holds? <a href="https://stackoverflow.com/questions/26103648/boostthis-threadinterruption-point-doesnt-throw-boostthread-interrupted">From another SO question on this topic</a>, it seems that the <code>boost::this_thread::interruption_point()</code> call might be what I'm looking for, but I'm not 100% sure if it will always work, from what I read in the SO question I referenced it from.</p>
  <hr />
  <p><strong>Finally,</strong> it's to my understanding that not calling any manner of boost <code>sleep()</code> function or some similar interruption point function in my loop will mean that a <code>timed_join()</code> will always timeout, and I'll either have to:</p>
  <ul>
  <li>Forcefully kill the thread.</li>
  <li>Fall back to using a mutex protected flag like in my original C code.</li>
  </ul>
  <p>Is this assumption correct?</p>
  <hr />
  <p>Thank you.</p>
  <hr />
  <h1>References</h1>
  <ol>
  <li><a href="https://stackoverflow.com/questions/7316633/boost-thread-how-to-acknowledge-interrupt">Boost Thread - How to acknowledge interrupt</a>, <code>&lt;https://stackoverflow.com/questions/7316633/boost-thread-how-to-acknowledge-interrupt&gt;</code>, Accessed 2016-01-18.</li>
  <li><a href="https://stackoverflow.com/questions/26103648/boostthis-threadinterruption-point-doesnt-throw-boostthread-interrupted">boost::this_thread::interruption_point() doesn't throw boost::thread_interrupted&amp; exception</a>, <code>&lt;https://stackoverflow.com/questions/26103648/boostthis-threadinterruption-point-doesnt-throw-boostthread-interrupted&gt;</code>, Accessed 2016-01-18.</li>
  </ol>