title: How to exit sleep() when an interrupt arrives?
body: <p>I'm looking for a way to exit sleep when an user interrupt arrives. It's important to <strong>exit</strong> sleep rather than do this: interrupt sleep, do ISR processing, and go back to sleep - which is what I'm finding solutions for.</p>
  <p>I'm looking for something like this in C++ - an equivalent in C is even better:</p>
  <pre><code>void *timer_thread(void *dummy)
  {
    while(1)
    {
      // Check if some callbacks are to be given
      // when all are given, Determine x duration to sleep
    
      try
      {
        sleep(x);
      }
      except(/* the except block should hit ONLY when an interrupt arrives, 
                that too only when sleep() is executed. It's OK to delay 
                interrupt until the sleep is beginning execution */) 
      {
        //Do something else
      }
    }
  }
  </code></pre>
  <p>The arriving interrupt will mostly tell that the sleep should be reduced for the timer thread to give callbacks earlier. But irrespective of the use case, I just need the sleep to be exited in some way when interrupt arrives. I just couldn't find info on how to do this.</p>
  <p>PS: It's ok to discard/NOP the interrupts if it occurs when sleep wasn't happening</p>
  <p>This is on Cygwin gcc v9.3.0 on Windows 10 (C/C++). I don't need the code to be portable, so any platform specific solution is ok as well.</p>
  <p>If there's some other solution which is similar to this working (which doesn't use sleep() and interrupts), would be welcome to hear it. I'm just looking for a way which doesn't involve polling.</p>