-
Notifications
You must be signed in to change notification settings - Fork 0
/
Thread.h
103 lines (80 loc) · 1.67 KB
/
Thread.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#ifndef THREAD_H
#define THREAD_H
#include <windows.h>
#include <stdio.h>
#include <stdint.h>
#include <iostream>
#include <tchar.h>
namespace threading{
class thread{
static DWORD WINAPI ThreadFunc(LPVOID pv){
try{
(reinterpret_cast<thread *>(pv))->run();
}
catch(...){
printf_s("Reinterpret failed.\n");
}
return 0;
}
public:
HANDLE hthread;
typedef DWORD threadid;
thread(){}
virtual ~thread(){}
static threadid getthreadid(){
return ::GetCurrentThreadId();
}
static void sleep(long milliseconds=1){
::Sleep(milliseconds);
}
threadid start(){
threadid id;
hthread = ::CreateThread(NULL, 0, ThreadFunc, this, 0, &id);
setPriority(THREAD_PRIORITY_NORMAL);
return id;
}
virtual void run()=0;
virtual void cleanup()=0;
void kill(){
if (hthread){
::TerminateThread( hthread, 0 );
::CloseHandle(hthread);
cleanup();
hthread = NULL;
}
}
void setPriority(int tp) {
if(hthread == NULL) {
printf_s("Thread object is null\n");
}else {
if(SetThreadPriority(hthread,tp) == 0) {
printf_s("Failed to set priority.\n");
}
}
}
};
};
class Mutex {
private:
// unsigned long* to the low-level mutex object
unsigned long* m_hMutex;
// name to identify the mutex
char m_strName[16];
public:
Mutex();
Mutex(const char* nm);
void create(const char* nm);
unsigned long* getMutexHandle();
char* getName();
void release();
~Mutex();
};
class Thread : public threading::thread{
public:
void (*funct)(void);
Thread();
virtual void bind(void *func);
virtual void run();
virtual void cleanup();
};
#endif