You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

threadinterrupt.h 920B

12345678910111213141516171819202122232425262728293031323334
  1. // Copyright (c) 2016 The Starwels developers
  2. // Distributed under the MIT software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #ifndef STARWELS_THREADINTERRUPT_H
  5. #define STARWELS_THREADINTERRUPT_H
  6. #include <atomic>
  7. #include <chrono>
  8. #include <condition_variable>
  9. #include <mutex>
  10. /*
  11. A helper class for interruptible sleeps. Calling operator() will interrupt
  12. any current sleep, and after that point operator bool() will return true
  13. until reset.
  14. */
  15. class CThreadInterrupt
  16. {
  17. public:
  18. explicit operator bool() const;
  19. void operator()();
  20. void reset();
  21. bool sleep_for(std::chrono::milliseconds rel_time);
  22. bool sleep_for(std::chrono::seconds rel_time);
  23. bool sleep_for(std::chrono::minutes rel_time);
  24. private:
  25. std::condition_variable cond;
  26. std::mutex mut;
  27. std::atomic<bool> flag;
  28. };
  29. #endif //STARWELS_THREADINTERRUPT_H