-
Notifications
You must be signed in to change notification settings - Fork 1
/
basicthread.h
83 lines (62 loc) · 1.76 KB
/
basicthread.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
/*
* File: customthread.h
* Author: siddharth
*
* Created on 10 July, 2012, 10:29 AM
*/
#ifndef BASICTHREAD_H
#define BASICTHREAD_H
class CBasicThread
{
private:
pthread_t m_nThreadId;
pthread_attr_t m_stThreadAttributes;
void* m_pThreadParam;
bool m_bShutdownFlag;
bool m_bIsExitComplete;
sem_t m_cThreadStartWait;
private:
public:
CBasicThread() : m_pThreadParam(NULL)
{
pthread_attr_init(&m_stThreadAttributes);
pthread_attr_setdetachstate(&m_stThreadAttributes, PTHREAD_CREATE_JOINABLE);
sem_init(&m_cThreadStartWait, 0, 0);
m_bShutdownFlag = false;
m_bIsExitComplete = false;
}
explicit CBasicThread(void* ThreadParam)
{
assert(ThreadParam);
m_pThreadParam = ThreadParam;
pthread_attr_init(&m_stThreadAttributes);
pthread_attr_setdetachstate(&m_stThreadAttributes, PTHREAD_CREATE_JOINABLE);
sem_init(&m_cThreadStartWait, 0, 0);
m_bShutdownFlag = false;
m_bIsExitComplete = false;
}
virtual ~CBasicThread();
RET_CODE Start();
RET_CODE Join();
inline void NotifyThreadStartEvent();
inline void* GetThreadParam()
{
return m_pThreadParam;
}
inline void SetShutdown()
{
m_bShutdownFlag = true;
m_bIsExitComplete = false;
}
inline bool IsThreadInShutdown()
{
return static_cast<bool>(m_bShutdownFlag);
}
inline pthread_t getthreadId()
{
return m_nThreadId;
}
virtual RET_CODE Process(void* pcThreadParam = NULL) = 0;
static void* ThreadProc(void* pcBasicThread);
};
#endif /* BASICTHREAD_H */