-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread.h
31 lines (24 loc) · 731 Bytes
/
thread.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
/* -*- mode: C++; c-file-style: "Google"; c-basic-offset: 4 -*- */
#pragma once
#include <atomic>
namespace cortisol {
class interrupt_exception : public std::exception {
public:
virtual const char *what() const noexcept { return "interrupted"; }
};
class thread_interrupter {
std::atomic_bool _interrupted;
public:
thread_interrupter() : _interrupted(false) {}
thread_interrupter(const thread_interrupter&) = delete;
thread_interrupter &operator=(const thread_interrupter&) = delete;
void interrupt() {
_interrupted.store(true);
}
void check_for_interrupt() {
if (_interrupted.load()) {
throw interrupt_exception();
}
}
};
} // namespace cortisol