title: Waking up thread from signal handler
body: <p>I understand that about the only thing, a signal handler in ISO/C++11 is allowed to do is to read from or write to a lock free atomic variable or a <code>volatile sig_atomic_t</code> (I believe, POSIX is a little bit more permissive and allows to call a bunch of system functions). </p>
  
  <p>I was wondering, if there is any way, to wake up a thread that is waiting on a condition variable. I.e. something like:</p>
  
  <pre><code>#include &lt;mutex&gt;
  #include &lt;atomic&gt;
  #include &lt;condition_variable&gt;
  
  
  std::mutex mux;
  std::condition_variable cv;
  
  std::atomic_bool doWait{ true };
  
  void signalHandler(int){
      doWait = false;
      cv.notify_one();
  }
  
  int main() {
      //register signal handler
      //Do some stuff
  
      {//wait until signal arrived
          std::unique_lock&lt;std::mutex&gt; ul(mux);
          cv.wait(ul, []{return !doWait; });
      }
  
      //Do some more stuff
  }
  </code></pre>
  
  <p>Which has at least two problems:</p>
  
  <ol>
  <li>I believe, I'm not allowed to call <code>notify_one()</code> in a signal handler (corrct me if I'm wrong)</li>
  <li>The signal could arrive just between the check for <code>doWait</code> and when the thread goes to sleep, so it would never wake up (obviously, I can't lock the mutex in the signalHander to avoid that). </li>
  </ol>
  
  <p>So far, the only solution I can see is to implement busy waiting on the <code>doWait</code> variable (probably sleep for a couple of milliseconds in each iteration), which strikes me as quite inefficient.  </p>
  
  <p>Note that even though, my program above has only one thread, I labeled my question with multithreading, because it is about thread control primitives. If there is no solution in standard c++ I'd be willing to accept a solution using Linux/POSIX specific functions. </p>