-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththread.h
93 lines (74 loc) · 1.5 KB
/
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
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
* thread.h
*
* Created on: 2011-10-26
* Author: gxl2007@hotmail.com
*/
#ifndef THREAD_H_
#define THREAD_H_
#include <stdint.h>
namespace xlnet
{
class thread
{
public:
thread():m_tid(0) { } ;
virtual ~thread() { } ;
public:
/*
* @brief create new thread and run
* @return 0 on success
*/
int start() ;
/*
* @brief join thread
*/
void join() ;
/*
* @brief thread id
*/
int64_t id() const { return m_tid ; } ;
protected:
/*
* @brief new thread callback , implemented by concrete class
*/
virtual void run() = 0 ;
private:
thread(const thread& o) ;
thread& operator=(const thread& o) ;
private:
static void* thread_entry(void* arg) ;
private:
int64_t m_tid ;
};
class simple_thread : public thread
{
public:
simple_thread():m_status(0) { } ;
virtual ~simple_thread() { } ;
/*
* @brief stop thread
*/
inline void stop() { m_status = 0 ; };
inline bool running() const { return m_status == 1 ; };
protected:
/*
* @brief called before run loop
* @return 0 on success
*/
virtual int on_init() { return 0 ; } ;
/*
* @brief called after run loop
*/
virtual void on_fini() { } ;
/*
* @brief called every loop
*/
virtual void run_once() = 0 ;
private:
void run() ;
private:
volatile int m_status ;
};
}
#endif /* THREAD_H_ */