1 // SGThread - Simple pthread class wrappers.
3 // Written by Bernie Bright, started April 2001.
5 // Copyright (C) 2001 Bernard Bright - bbright@bigpond.net.au
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #ifndef SGTHREAD_HXX_INCLUDED
24 #define SGTHREAD_HXX_INCLUDED 1
33 void* start_handler( void* );
37 * Encapsulate generic threading methods.
38 * Users derive a class from SGThread and implement the run() member function.
74 virtual void run() = 0;
83 friend void* start_handler( void* );
87 SGThread( const SGThread& );
88 SGThread& operator=( const SGThread& );
104 int status = pthread_create( &tid, 0, start_handler, this );
105 assert( status == 0 );
112 int status = pthread_join( tid, 0 );
113 assert( status == 0 );
119 int status = pthread_cancel( tid );
120 assert( status == 0 );
124 * A mutex is used to protect a section of code such that at any time
125 * only a single thread can execute the code.
129 friend class SGCondition;
134 * Create a new mutex.
139 * Destroy a mutex object.
159 pthread_mutex_t mutex;
162 inline SGMutex::SGMutex()
164 int status = pthread_mutex_init( &mutex, 0 );
165 assert( status == 0 );
168 inline SGMutex::~SGMutex()
170 int status = pthread_mutex_destroy( &mutex );
171 assert( status == 0 );
174 inline void SGMutex::lock()
176 int status = pthread_mutex_lock( &mutex );
177 assert( status == 0 );
180 inline void SGMutex::unlock()
182 int status = pthread_mutex_unlock( &mutex );
183 assert( status == 0 );
193 * Create a new condition variable.
198 * Destroy the condition object.
203 * Wait for this condition variable to be signaled.
205 * @param SGMutex& reference to a locked mutex.
207 void wait( SGMutex& );
210 * Wait for this condition variable to be signaled for at most
213 * @param SGMutex& reference to a locked mutex.
214 * @param unsigned long milliseconds to wait for a signal.
218 bool wait( SGMutex& mutex, unsigned long ms );
221 * Wake one thread waiting on this condition variable.
226 * Wake all threads waiting on this condition variable.
232 SGCondition(const SGCondition& );
233 SGCondition& operator=(const SGCondition& );
240 inline SGCondition::SGCondition()
242 int status = pthread_cond_init( &cond, 0 );
243 assert( status == 0 );
246 inline SGCondition::~SGCondition()
248 int status = pthread_cond_destroy( &cond );
249 assert( status == 0 );
252 inline void SGCondition::signal()
254 int status = pthread_cond_signal( &cond );
255 assert( status == 0 );
258 inline void SGCondition::broadcast()
260 int status = pthread_cond_broadcast( &cond );
261 assert( status == 0 );
264 inline void SGCondition::wait( SGMutex& mutex )
266 int status = pthread_cond_wait( &cond, &mutex.mutex );
267 assert( status == 0 );
270 #endif /* SGTHREAD_HXX_INCLUDED */