-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTThread.hh
executable file
·282 lines (220 loc) · 6.42 KB
/
TThread.hh
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
272
273
274
275
276
277
278
279
280
281
282
#ifndef __TTHREAD_HH
#define __TTHREAD_HH
//
// Project : ThreadPool
// File : TThread.hh
// Author : Ronald Kriemann
// Purpose : baseclass for a thread-able class
//
#include <cstdio>
#include <pthread.h>
namespace ThreadPool
{
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
//!
//! \class TThread
//! \brief baseclass for all threaded classes
//! - defines basic interface
//!
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
class TThread
{
protected:
//! @cond
// thread-specific things
pthread_t _thread_id;
// is the thread running or not
bool _running;
// no of thread
int _thread_no;
//! @endcond
public:
////////////////////////////////////////////
//
// constructor and destructor
//
//! construct thread with thread number \a thread_no
TThread ( const int thread_no = -1 );
//! destruct thread, if thread is running, it will be canceled
virtual ~TThread ();
////////////////////////////////////////////
//
// access local data
//
//! return thread number
int thread_no () const { return _thread_no; }
//! set thread number to \a n
void set_thread_no ( const int n );
//! compare if processor number \a p is local one
bool on_proc ( const int p ) const
{
return ((p == -1) || (_thread_no == -1) || (p == _thread_no));
}
////////////////////////////////////////////
//
// user-interface
//
//! actual method to be executed by thread
virtual void run () = 0;
////////////////////////////////////////////
//
// thread management
//
//! create thread (actually start it);
//! - if \a detached is true, the thread will be started in detached mode,
//! e.g. can not be joined,
//! - if \a sscope is true, the thread is started in system scope, e.g.
//! the thread competes for resources with all other threads of all
//! processes on the system; if \a sscope is false, the competition is
//! only process local
void create ( const bool detached = false,
const bool sscope = false );
//! detach thread
void detach ();
//! synchronise with thread (wait until finished)
void join ();
//! request cancellation of thread
void cancel ();
protected:
//! @cond
////////////////////////////////////////////
//
// functions to be called by a thread itself
//
//! terminate thread
void exit ();
//! put thread to sleep for <sec> seconds
void sleep ( const double sec );
public:
////////////////////////////////////////////
//
// internally used, but public functions
//
//! resets running-status (used in _run_proc, see TThread.cc)
void reset_running () { _running = false; }
//! @endcond
};
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
//
// wrapper for pthread_mutex
//
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
class TMutex
{
protected:
//! @cond
// the mutex itself and the mutex-attr
pthread_mutex_t _mutex;
pthread_mutexattr_t _mutex_attr;
//! @endcond
public:
/////////////////////////////////////////////////
//
// constructor and destructor
//
//! construct unlocked mutex
TMutex ()
{
pthread_mutexattr_init( & _mutex_attr );
pthread_mutex_init( & _mutex, & _mutex_attr );
}
//! dtor
~TMutex ()
{
pthread_mutex_destroy( & _mutex );
pthread_mutexattr_destroy( & _mutex_attr );
}
/////////////////////////////////////////////////
//
// usual behavior of a mutex
//
//! lock mutex
void lock () { pthread_mutex_lock( & _mutex ); }
//! unlock mutex
void unlock () { pthread_mutex_unlock( & _mutex ); }
//! return true if mutex is locked and false, otherwise
bool is_locked ()
{
if ( pthread_mutex_trylock( & _mutex ) != 0 )
return true;
else
{
unlock();
return false;
}// else
}
};
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
//!
//! \class TScopedLock
//! \brief Provides automatic lock and unlock for mutices.
//!
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
class TScopedLock
{
private:
//! @cond
//! given mutex for locking
TMutex * _mutex;
// prevent copy operations
TScopedLock ( TScopedLock & );
void operator = ( TScopedLock & );
//! @endcond
public:
//! ctor: lock mutex
explicit TScopedLock ( TMutex & m )
: _mutex( & m )
{
_mutex->lock();
}
//! dtor: unlock mutex
~TScopedLock ()
{
_mutex->unlock();
}
};
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
//!
//! \class TCondition
//! \brief class for a condition variable
//! - derived from mutex to allow locking of condition
//! to inspect or modify the predicate
//!
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
class TCondition : public TMutex
{
private:
//! @cond
// our condition variable
pthread_cond_t _cond;
//! @endcond
public:
/////////////////////////////////////////////////
//
// constructor and destructor
//
//! ctor
TCondition () { pthread_cond_init( & _cond, NULL ); }
//! dtor
~TCondition () { pthread_cond_destroy( & _cond ); }
/////////////////////////////////////////////////
//
// condition variable related methods
//
//! wait for signal to arrive
void wait () { pthread_cond_wait( & _cond, & _mutex ); }
//! restart one of the threads, waiting on the cond. variable
void signal () { pthread_cond_signal( & _cond ); }
//! restart all waiting threads
void broadcast () { pthread_cond_broadcast( & _cond ); }
};
}// namespace ThreadPool
#endif // __TTHREAD_HH