-
Notifications
You must be signed in to change notification settings - Fork 0
/
Thread.hpp
79 lines (66 loc) · 1.5 KB
/
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#ifndef THREAD_H
#define THREAD_H
#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#define THREAD_RET DWORD WINAPI
#else
#include <pthread.h>
#define THREAD_RET void *
#endif
#include "Mutex.hpp"
THREAD_RET thread_start(void * obj);
class Thread
{
public:
Thread()
{
_quit = false;
_quitMutex = new Mutex();
}
virtual ~Thread()
{
delete _quitMutex;
}
virtual void run() = 0;
void startThread()
{
#ifdef WIN32
DWORD threadID;
_thread = CreateThread(NULL, 0, thread_start, (void*)this, 0, &threadID);
SetThreadPriority(_thread, THREAD_PRIORITY_HIGHEST);
#else
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
pthread_create(&_thread, &attr, thread_start, (void*)this);
pthread_attr_destroy(&attr);
#endif
}
void quit()
{
_quitMutex->lock();
_quit = true;
_quitMutex->unlock();
}
void join()
{
quit();
#ifdef WIN32
WaitForSingleObject(_thread, INFINITE);
CloseHandle(_thread);
#else
void * status;
pthread_join(_thread, &status);
#endif
}
protected:
bool _quit;
Mutex * _quitMutex;
#ifdef WIN32
HANDLE _thread;
#else
pthread_t _thread;
#endif
};
#endif