forked from caozhiyi/foundation
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Runnable.h
47 lines (39 loc) · 806 Bytes
/
Runnable.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
#ifndef HEADER_RUNNABLE
#define HEADER_RUNNABLE
#include <thread>
class CRunnable
{
public:
CRunnable() : _stop(false) {}
virtual ~CRunnable() {}
//线程基本操作
virtual void Start() {
_stop = false;
if (!_pthread) {
_pthread = std::shared_ptr<std::thread>(new std::thread(std::bind(&CRunnable::Run, this)));
}
}
virtual void Stop() {
_stop = true;
}
virtual void Join() {
if (_pthread) {
_pthread->join();
}
}
//线程主逻辑
virtual void Run() = 0;
bool GetStop() {
return _stop;
}
static void Sleep(int interval) {
std::this_thread::sleep_for(std::chrono::milliseconds(interval));
}
protected:
CRunnable(const CRunnable&) = delete;
CRunnable& operator=(const CRunnable&) = delete;
protected:
bool _stop;
std::shared_ptr<std::thread> _pthread;
};
#endif