2

Is there any way to "cancel" the:

std::this_thread::sleep_until(now + std::chrono::milliseconds(200));

Let's imagine the following scenario:

I set my thread to wake up in 200ms... but after 10ms, I receive a new request that my thread must wake up in 100ms...

So the thread should wake up in: 100ms and then at 200ms (after the current time, of course).

Thanks :)

waas1919
  • 2,365
  • 7
  • 44
  • 76
  • 1
    wouldn't this answer your question? http://stackoverflow.com/questions/12025015/how-do-i-wake-up-a-sleeping-pthread – wgitscht Jul 15 '15 at 11:25
  • 1
    That question with corresponding answers are related to the pthread library. And this question is about the C++ standard threads. – Kirill V. Lyadvinsky Jul 15 '15 at 11:42
  • 1
    You can't but have a look at [`std::condition_variable`](http://en.cppreference.com/w/cpp/thread/condition_variable) which allows you to notify threads depending on a certain condition. – Pixelchemist Jul 15 '15 at 11:57
  • This answer shows how to do it with `std::conditional_variable` : https://stackoverflow.com/questions/29775153/stopping-long-sleep-threads – AdamF Nov 10 '21 at 08:23

1 Answers1

8

I don't think you can do this with sleep_until. Have a look at condition variables ( http://www.cplusplus.com/reference/condition_variable/condition_variable/ ). Should be usable for exactly what you want.

graywolf
  • 7,092
  • 7
  • 53
  • 77