-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker_thread.hpp
58 lines (43 loc) · 1.03 KB
/
worker_thread.hpp
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
#ifndef VI_WORKER_THREAD_HPP
#define VI_WORKER_THREAD_HPP
#include "common_inc.h"
namespace vi
{
template<class T>
class worker_thread
{
private:
T mTask;
pthread_t mThread;
bool mThreadCreated;
public:
worker_thread(const T& task) : mTask(task),
mThread(0),
mThreadCreated(false)
{
}
void start()
{
mThreadCreated = (0 == pthread_create(&mThread, NULL, do_work, this));
}
void join()
{
if (mThreadCreated)
{
pthread_join(mThread, NULL);
}
}
private:
static void* do_work(void* pVoidThis)
{
worker_thread<T>* pThis = reinterpret_cast<worker_thread<T>*>(pVoidThis);
pThis->mTask.begin();
while(pThis->mTask.step())
{
}
pThis->mTask.end();
return NULL;
}
};
};
#endif