LIPH's C++ Codes
thread.h
Go to the documentation of this file.
1#ifndef LIPH_CONCURRENCY_THREAD_H_
2#define LIPH_CONCURRENCY_THREAD_H_
3
4#include <thread>
5
6// thread_guard
7// scoped_thread
8
9namespace liph {
10
11inline void sleeps(int n) { std::this_thread::sleep_for(std::chrono::seconds(n)); }
12inline void sleepms(int n) { std::this_thread::sleep_for(std::chrono::microseconds(n)); }
13
15public:
16 explicit thread_guard(std::thread& t) : t_(t) {}
18 if (t_.joinable()) {
19 t_.join();
20 }
21 }
22 thread_guard(const thread_guard&) = delete;
24
25private:
26 std::thread& t_;
27};
28
30public:
31 explicit scoped_thread(std::thread t_) : t(std::move(t_)) {
32 if (!t.joinable()) throw std::logic_error("No thread");
33 }
34 ~scoped_thread() { t.join(); }
35 scoped_thread(scoped_thread const&) = delete;
37
38private:
39 std::thread t;
40};
41
42} // namespace liph
43
44#endif // LIPH_CONCURRENCY_THREAD_H_
Definition: thread.h:29
scoped_thread(scoped_thread const &)=delete
scoped_thread & operator=(scoped_thread const &)=delete
scoped_thread(std::thread t_)
Definition: thread.h:31
~scoped_thread()
Definition: thread.h:34
Definition: thread.h:14
~thread_guard()
Definition: thread.h:17
thread_guard(std::thread &t)
Definition: thread.h:16
thread_guard & operator=(const thread_guard &)=delete
thread_guard(const thread_guard &)=delete
Definition: algorithm.h:10
void sleeps(int n)
Definition: thread.h:11
void sleepms(int n)
Definition: thread.h:12