title: Interrupting a program (SIGINT) during a condition_variable::wait() call, with a subsequent call to exit(), causes it to freeze
body: <p>I'm not sure I understand this issue very well, so I've written a small example program that demonstrates it:</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;csignal&gt;
  #include &lt;mutex&gt;
  #include &lt;condition_variable&gt;
  #include &lt;thread&gt;
  
  class Application {
      std::mutex cvMutex;
      std::condition_variable cv;
      std::thread t2;
      bool ready = false;
  
      // I know I'm accessing this without a lock, please ignore that
      bool shuttingDown = false;
  
  public:
      void mainThread() {
          auto lock = std::unique_lock&lt;std::mutex&gt;(this-&gt;cvMutex);
  
          while (!this-&gt;shuttingDown) {
              if (!this-&gt;ready) {
                  std::cout &lt;&lt; &quot;Main thread waiting.\n&quot; &lt;&lt; std::flush;
                  this-&gt;cv.wait(lock, [this] () {return this-&gt;ready;});
              }
  
              // Do the thing
              this-&gt;ready = false;
              std::cout &lt;&lt; &quot;Main thread notification recieved.\n&quot; &lt;&lt; std::flush;
          }
      };
  
      void notifyMainThread() {
          std::cout &lt;&lt; &quot;Notifying main thread.\n&quot; &lt;&lt; std::flush;
          this-&gt;cvMutex.lock();
          this-&gt;ready = true;
          this-&gt;cv.notify_all();
          this-&gt;cvMutex.unlock();
          std::cout &lt;&lt; &quot;Notified.\n&quot; &lt;&lt; std::flush;
      };
  
      void threadTwo() {
          while(!this-&gt;shuttingDown) {
              // Wait some seconds, then notify main thread
              std::cout &lt;&lt; &quot;Thread two sleeping for some seconds.\n&quot; &lt;&lt; std::flush;
              std::this_thread::sleep_for(std::chrono::seconds(3));
              std::cout &lt;&lt; &quot;Thread two calling notifyMainThread().\n&quot; &lt;&lt; std::flush;
              this-&gt;notifyMainThread();
          }
  
          std::cout &lt;&lt; &quot;Thread two exiting.\n&quot; &lt;&lt; std::flush;
      };
  
      void run() {
          this-&gt;t2 = std::thread(&amp;Application::threadTwo, this);
          this-&gt;mainThread();
  
      };
  
      void shutdown() {
          this-&gt;shuttingDown = true;
          this-&gt;notifyMainThread();
          std::cout &lt;&lt; &quot;Joining thread two.\n&quot; &lt;&lt; std::flush;
          this-&gt;t2.join();
          std::cout &lt;&lt; &quot;Thread two joined.\n&quot; &lt;&lt; std::flush;
          // The following call causes the program to hang when triggered by a signal handler
          exit(EXIT_SUCCESS);
      }
  };
  
  auto app = Application();
  int sigIntCount = 0;
  
  int main(int argc, char *argv[])
  {
      std::signal(SIGINT, [](int signum) {
          std::cout &lt;&lt; &quot;SIGINT recieved!\n&quot; &lt;&lt; std::flush;
          sigIntCount++;
          if (sigIntCount == 1) {
              // First SIGINT recieved, attempt a clean shutdown
              app.shutdown();
          } else {
              abort();
          }
      });
  
      app.run();
  
      return 0;
  }
  </code></pre>
  <p>You can run the program online, here: <a href="https://onlinegdb.com/Bkjf-4RHP" rel="nofollow noreferrer">https://onlinegdb.com/Bkjf-4RHP</a></p>
  <p>The example above is a simple multithreaded application that consists of two threads. The main thread waits on a condition variable until a notification is received and <code>this-&gt;ready</code> has been set to <code>true</code>. The second thread simply updates <code>this-&gt;ready</code> and notifies the main thread, periodically. And finally, the application handles SIGINT <strong>on the main thread</strong>, where it attempts to perform a clean shutdown.</p>
  <p><strong>The issue:</strong></p>
  <p>When a SIGINT is triggered (via Ctrl+C), the application does not exit, despite calling <code>exit()</code> in <code>Application::shutdown()</code>.</p>
  <p>This is what I think is happening:</p>
  <ol>
  <li>The main thread is waiting for a notification, so it's being blocked by <code>this-&gt;cv.wait(lock, [this] () {return this-&gt;ready;});</code></li>
  <li>SIGINT is received, and the <code>wait()</code> call is interrupted by the signal, which results in the signal handler being invoked.</li>
  <li>The signal handler calls <code>Application::shutdown()</code>, which subsequently calls <code>exit()</code>. The call to <code>exit()</code> hangs indefinitely because it's attempting some cleanup that cannot be achieved until the <code>wait()</code> call resumes (I'm not sure about this).</li>
  </ol>
  <p>I'm really not sure about that last point, but this is why I think it's the case:</p>
  <ul>
  <li>When I remove the call to <code>exit()</code> in <code>Application::shutdown()</code> and let the <code>main()</code> return, the program exits without issue.</li>
  <li>When I replace the call to <code>exit()</code> with <code>abort()</code>, which does less in respect to cleanup, the program exits without issue (so this indicates that the cleanup process conducted by exit() is resulting in the freezing).</li>
  <li>If the SIGINT is sent when the main thread is <strong>not</strong> waiting on the condition variable, the program exits without issue.</li>
  </ul>
  <p>The above is just an example of the issue I'm having. In my case, I need to call <code>exit()</code> in <code>shutdown()</code>, and <code>shutdown()</code> needs to be invoked from the signal handler. So far, my options seem to be:</p>
  <ul>
  <li>Move all signal handing into a dedicated thread. This would be a pain to do as it would require rewriting the code to enable me to call <code>Application::shutdown()</code> from a different thread as to the one that owns the instance to <code>Application</code>. I'd also need a way to pull the main thread out of the <code>wait()</code> call, likely by adding some <code>OR</code> condition to the predicate.</li>
  <li>Replace the call to <code>exit()</code> with a call to <code>abort()</code>. This would work but would result in the stack not being unwound (specifically, the <code>Application</code> instance).</li>
  </ul>
  <p>Do I have any other options? Is there any way to properly interrupt a thread during a call to <code>std::condition_variable::wait()</code>, and exit the program from within the interrupt handler?</p>