-
Notifications
You must be signed in to change notification settings - Fork 1
/
thread.cpp
49 lines (40 loc) · 782 Bytes
/
thread.cpp
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
#include "thread.h"
Thread::Thread(CallFunc func) : m_bStarted(false), m_bJointed(false), m_pthreadId(0), m_func(func)
{
}
Thread::~Thread()
{
if (m_bStarted && !m_bJointed)
{
pthread_detach(m_pthreadId);
}
}
struct ThreadData
{
typedef typename Thread::CallFunc CallFunc;
ThreadData(CallFunc func) : m_func(func) {}
CallFunc m_func;
};
void * ThreadRun(void * arg)
{
ThreadData * ptData = static_cast<ThreadData *> (arg);
ptData->m_func();
delete ptData;
}
void Thread::Start()
{
if (!m_bStarted)
{
ThreadData * ptData = new ThreadData(m_func);
pthread_create(&m_pthreadId, NULL, ThreadRun, static_cast<void *> (ptData));
m_bStarted = true;
}
}
void Thread::Join()
{
if (!m_bJointed)
{
pthread_join(m_pthreadId, NULL);
m_bJointed = true;
}
}