This repository was archived by the owner on Feb 16, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRAIIThread.h
More file actions
60 lines (44 loc) · 1.44 KB
/
Copy pathRAIIThread.h
File metadata and controls
60 lines (44 loc) · 1.44 KB
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
#ifndef RAIITHREAD_H
#define RAIITHREAD_H
#include <thread>
class RAIIThread{
public:
RAIIThread() = default;
template<class Function, class... Args>
explicit RAIIThread(Function&& F, Args... args) noexcept : thread_(F, args...){}
RAIIThread(RAIIThread&& other) noexcept : thread_(std::move(other.thread_)) {}
/* Thread does not have a copy constructor */
RAIIThread(const RAIIThread& t) = delete;
~RAIIThread(){
if(thread_.joinable()){
thread_.join();
}
}
constexpr void join() {
thread_.join();
}
constexpr void detach() {
thread_.detach();
}
inline bool joinable() const noexcept {
return thread_.joinable();
}
[[nodiscard]] inline std::thread::id get_id() const noexcept {
return thread_.get_id();
}
inline auto native_handle() {
return thread_.native_handle();
}
constexpr void swap(RAIIThread& other) noexcept{
thread_.swap(other.thread_);
}
inline RAIIThread& operator=(RAIIThread&& other) noexcept {
thread_ = std::move(other.thread_);
return *this;
}
/* Thread does not have an assignment operator */
RAIIThread& operator=(const RAIIThread&) = delete;
private:
std::thread thread_;
};
#endif //RAIITHREAD_H