1 #ifndef _QUITTABLE_SLEEPER
2 #define _QUITTABLE_SLEEPER 1
4 // A class that assists with fast shutdown of threads. You can set
5 // a flag that says the thread should quit, which it can then check
6 // in a loop -- and if the thread sleeps (using the sleep_* functions
7 // on the class), that sleep will immediately be aborted.
9 // All member functions on this class are thread-safe.
14 class QuittableSleeper {
18 std::lock_guard<std::mutex> l(mu);
19 should_quit_var = true;
20 quit_cond.notify_all();
25 std::lock_guard<std::mutex> l(mu);
26 should_quit_var = false;
31 std::lock_guard<std::mutex> l(mu);
32 should_wakeup_var = true;
33 quit_cond.notify_all();
36 bool should_quit() const
38 std::lock_guard<std::mutex> l(mu);
39 return should_quit_var;
42 // Returns false if woken up early.
43 template<class Rep, class Period>
44 bool sleep_for(const std::chrono::duration<Rep, Period> &duration)
46 std::chrono::steady_clock::time_point t =
47 std::chrono::steady_clock::now() +
48 std::chrono::duration_cast<std::chrono::steady_clock::duration>(duration);
49 return sleep_until(t);
52 // Returns false if woken up early.
53 template<class Clock, class Duration>
54 bool sleep_until(const std::chrono::time_point<Clock, Duration> &t)
56 std::unique_lock<std::mutex> lock(mu);
57 quit_cond.wait_until(lock, t, [this]{
58 return should_quit_var || should_wakeup_var;
60 if (should_wakeup_var) {
61 should_wakeup_var = false;
64 return !should_quit_var;
68 mutable std::mutex mu;
69 bool should_quit_var = false, should_wakeup_var = false;
70 std::condition_variable quit_cond;
73 #endif // !defined(_QUITTABLE_SLEEPER)