forked from twohoursonelife/minorGems
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathThread.h
136 lines (102 loc) · 2.87 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* Modification History
*
* 2000-December-13 Jason Rohrer
* Created.
*
* 2001-March-4 Jason Rohrer
* Made sleep() static so it can be called by non-Thread classes.
*
* 2001-May-12 Jason Rohrer
* Added comments about joining before destroying.
*
* 2002-March-29 Jason Rohrer
* Added Fortify inclusion.
*
* 2002-August-5 Jason Rohrer
* Made destructor virtual.
*
* 2004-March-31 Jason Rohrer
* Added support for detatched mode.
*
* 2005-January-9 Jason Rohrer
* Made sleep function virtual to allow overrides.
*
* 2005-January-22 Jason Rohrer
* Added a static sleep function.
*
* 2011-March-9 Jason Rohrer
* Removed Fortify inclusion.
*/
#include "minorGems/common.h"
#ifndef THREAD_CLASS_INCLUDED
#define THREAD_CLASS_INCLUDED
/**
* Base class to be subclassed by all threads.
*
* Note: Implementation for the functions defined here is provided
* separately for each platform (in the mac/ linux/ and win32/
* subdirectories).
*
* @author Jason Rohrer
*/
class Thread {
public:
Thread();
virtual ~Thread();
/**
* Starts this Thread.
*
* Note that after starting a non-detached thread, it _must_ be
* joined before being destroyed to avoid memory leaks.
*
* Threads running in detatched mode handle their own destruction
* as they terminate and do not need to be joined at all.
*
* @param inDetach true if this thread should run in detatched mode,
* or false to run in non-detached mode. Defaults to false.
*/
void start( char inDetach = false );
/**
* To be overriden by subclasses.
* This method will be run by the Thread after start() has been called.
*/
virtual void run() = 0;
/**
* Blocks until this thread finishes executing its run() method.
*
* Must be called before destroying this thread, if this thread
* has been started.
*/
void join();
/**
* Puts the current thread to sleep for a specified amount of time.
*
* Note that given a thread instance threadA, calling threadA.sleep()
* will put the calling thread to sleep.
*
* @param inTimeInMilliseconds the number of milliseconds to sleep.
*/
virtual void sleep( unsigned long inTimeInMilliseconds ) {
staticSleep( inTimeInMilliseconds );
}
/**
* Same as sleep, but can be called without constructing a thread.
*/
static void staticSleep( unsigned long inTimeInMilliseconds );
/**
* Gets whether this thread is detached.
*
* @return true if this thread is detached.
*/
char isDetatched() {
return mIsDetached;
}
private:
/**
* Used by platform-specific implementations.
*/
void *mNativeObjectPointer;
char mIsDetached;
};
#endif