-
Notifications
You must be signed in to change notification settings - Fork 5
/
threadtest.cpp
272 lines (226 loc) · 5.04 KB
/
threadtest.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include <iostream>
#include <cstring>
#include <unistd.h>
#include "config.h"
#include "support/debug.h"
#include "support/progressthread.h"
#include "support/threadevent.h"
#include "rwmutex.h"
#include "breakhandler.h"
using namespace std;
#if 0
class TestThread : public ThreadFunction
{
public:
TestThread() : ThreadFunction(), thread(this)
{
cerr << "Starting thread" << endl;
thread.Start();
cerr << "Started" << endl;
}
virtual ~TestThread()
{
cerr << "Waiting for thread to finish" << endl;
thread.WaitFinished();
cerr << "Thread finished" << endl;
}
virtual int Entry(Thread &t)
{
#ifdef WIN32
Sleep(1000);
#else
sleep(1);
#endif
return(0);
}
protected:
Thread thread;
};
int main(int argc, char **argv)
{
TestThread t;
return(0);
}
#endif
#if 1
//------------------------------------
class TestThread_WaitTH : public ThreadFunction
{
public:
TestThread_WaitTH(ThreadEvent &event) : ThreadFunction(), event(event), thread(this)
{
thread.Start();
}
virtual ~TestThread_WaitTH()
{
cerr << "Destructing WaitTH" << endl;
thread.WaitFinished();
}
virtual int Entry(Thread &t)
{
// cerr << "Thread " << t.GetThreadID() << " initializing" << endl;
event.WaitAndHold();
// cerr << "Thread " << t.GetThreadID() << " pausing..." << endl;
#ifdef WIN32
Sleep(1000);
#else
sleep(1);
#endif
event.Release();
// cerr << "Thread " << t.GetThreadID() << " exiting" << endl;
return(0);
}
protected:
ThreadEvent &event;
Thread thread;
};
class TestThread_SendTH : public ThreadFunction
{
public:
TestThread_SendTH(ThreadEvent &event) : ThreadFunction(), event(event), thread(this)
{
thread.Start();
}
virtual ~TestThread_SendTH()
{
cerr << "Destructing SendTH" << endl;
thread.WaitFinished();
}
virtual int Entry(Thread &t)
{
// cerr << "Thread " << t.GetThreadID() << " initializing" << endl;
event.Trigger();
// cerr << "Thread " << t.GetThreadID() << " exiting" << endl;
return(0);
}
protected:
ThreadEvent &event;
Thread thread;
};
int main(int argc, char **argv)
{
Debug.SetLevel(TRACE);
if(BreakHandler.TestBreak())
std::cerr << "Break signal received..." << std::endl;
else
std::cerr << "No break signal received..." << std::endl;
ThreadEventHandler tehandler;
ThreadEvent e1(tehandler,"Event1");
ThreadEvent *e2=new ThreadEvent(tehandler,"Event2");
e1.Subscribe(); // Subscribing allows us to keep track of how many times the signal's
// been received, and more importantly, whether it was received before we started waiting.
TestThread_WaitTH tt1(e1);
TestThread_WaitTH tt2(e1);
TestThread_WaitTH tt3(e1);
TestThread_WaitTH tt4(e1);
cerr << "Sending signal..." << endl;
TestThread_SendTH tt5(e1);
cerr << "Main thread waiting..." << endl;
e1.QueryAndWait();
cerr << "Main thread woken" << endl;
return(0);
}
#endif
#if 0
class TestThread1 : public ThreadFunction, public Thread
{
public:
TestThread1(RWMutex &mutex) : ThreadFunction(), Thread(this), mutex(mutex)
{
cerr << "Creating TestThread1" << endl;
}
virtual ~TestThread1()
{
cerr << "Disposing of TestThread1" << endl;
}
virtual int Entry(Thread &t)
{
{
PTMutex::Lock lock1(mutex,false);
Debug[TRACE] << "Subthread attempting write lock" << endl;
if(lock1.Attempt())
{
Debug[TRACE] << "Obtained write-lock" << endl;
}
else
Debug[TRACE] << "Subthread Can't get write lock - read lock held elsewhere?" << endl;
}
RWMutex::SharedLock lock2(mutex);
Debug[TRACE] << "Subthread Got shared lock - about to sleep" << endl;
ProgressThread p(*this);
SendSync();
for(int i=0;i<20;++i)
{
#ifdef WIN32
Sleep(20);
#else
usleep(20000);
#endif
if(!p.DoProgress())
{
Debug[TRACE] << "Thread cancelled - exiting" << endl;
return(0);
}
}
t.SendSync();
#ifdef WIN32
Sleep(5000);
#else
sleep(5);
#endif
cerr << "Thread finished sleeping - exiting" << endl;
return(0);
}
protected:
RWMutex &mutex;
};
class TestThread2 : public ThreadFunction, public Thread
{
public:
TestThread2(RWMutex &mutex) : ThreadFunction(), Thread(this), mutex(mutex)
{
cerr << "Creating TestThread1" << endl;
}
virtual ~TestThread2()
{
cerr << "Disposing of TestThread1" << endl;
}
virtual int Entry(Thread &t)
{
t.SendSync();
cerr << "Subthread attempting write lock" << endl;
{
RWMutex::SharedLock lock(mutex);
cerr << "Sub-thread about to sleep..." << endl;
#ifdef WIN32
Sleep(50);
#else
usleep(50000);
#endif
}
cerr << "Woken up - attempting exclusive lock (with shared lock still held)" << endl;
cerr << "Thread finished sleeping - exiting" << endl;
return(0);
}
protected:
RWMutex &mutex;
};
int main(int argc, char **argv)
{
Debug.SetLevel(ERROR);
RWMutex mutex;
TestThread1 tt1(mutex);
{
// Obtaining shared lock...
Debug[TRACE] << "Obtaining shared lock from main thread..." << endl;
RWMutex::SharedLock lock(mutex);
Debug[TRACE] << "Starting thread..." << endl;
tt1.Start();
tt1.WaitSync();
Debug[TRACE] << "Main thread received signal..." << endl;
}
tt1.WaitFinished();
Debug[TRACE] << "Done" << endl;
return(0);
}
#endif