-
Notifications
You must be signed in to change notification settings - Fork 1
/
base.h
81 lines (61 loc) · 1.2 KB
/
base.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
/*
* Copyright (C) [email protected]
*/
#ifndef __BASE_INCLUDE__
#define __BASE_INCLUDE__
#include <pthread.h>
class BaseThread {
public:
BaseThread() {
}
virtual ~BaseThread() {
}
void create();
int wait() {
return pthread_join(_thread_id, NULL);
}
virtual bool stop() {
return true;
};
pthread_t get_tid() {
return _thread_id;
}
protected:
virtual int do_thread_func() = 0;
static void *thread_func(void *arg);
private:
pthread_t _thread_id;
};
class ThreadCond {
public:
ThreadCond() {
}
~ThreadCond() {
destroy();
}
bool init() {
if (pthread_mutex_init(&_cond_lock, NULL) != 0)
return false;
if (pthread_cond_init(&_cond, NULL) != 0)
return false;
return true;
}
void destroy() {
pthread_mutex_destroy(&_cond_lock);
pthread_cond_destroy(&_cond);
}
void wait() {
pthread_mutex_lock(&_cond_lock);
pthread_cond_wait(&_cond, &_cond_lock);
pthread_mutex_unlock(&_cond_lock);
}
void notify() {
pthread_mutex_lock(&_cond_lock);
pthread_cond_signal(&_cond);
pthread_mutex_unlock(&_cond_lock);
}
private:
pthread_mutex_t _cond_lock;
pthread_cond_t _cond;
};
#endif