-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathInterruptableSleep.h
63 lines (48 loc) · 1.87 KB
/
InterruptableSleep.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#pragma once
#include <chrono>
#include <thread>
#include <future>
#include <mutex>
#include <sstream>
namespace Bosma {
class InterruptableSleep {
using Clock = std::chrono::system_clock;
// InterruptableSleep offers a sleep that can be interrupted by any thread.
// It can be interrupted multiple times
// and be interrupted before any sleep is called (the sleep will immediately complete)
// Has same interface as condition_variables and futures, except with sleep instead of wait.
// For a given object, sleep can be called on multiple threads safely, but is not recommended as behaviour is undefined.
public:
InterruptableSleep() : interrupted(false) {
}
InterruptableSleep(const InterruptableSleep &) = delete;
InterruptableSleep(InterruptableSleep &&) noexcept = delete;
~InterruptableSleep() noexcept = default;
InterruptableSleep &operator=(const InterruptableSleep &) noexcept = delete;
InterruptableSleep &operator=(InterruptableSleep &&) noexcept = delete;
void sleep_for(Clock::duration duration) {
std::unique_lock<std::mutex> ul(m);
cv.wait_for(ul, duration, [this] { return interrupted; });
interrupted = false;
}
void sleep_until(Clock::time_point time) {
std::unique_lock<std::mutex> ul(m);
cv.wait_until(ul, time, [this] { return interrupted; });
interrupted = false;
}
void sleep() {
std::unique_lock<std::mutex> ul(m);
cv.wait(ul, [this] { return interrupted; });
interrupted = false;
}
void interrupt() {
std::lock_guard<std::mutex> lg(m);
interrupted = true;
cv.notify_one();
}
private:
bool interrupted;
std::mutex m;
std::condition_variable cv;
};
}