fixed the Mutex class to make it thread-safe (I hope)

This commit is contained in:
fgerlits 2006-12-02 08:30:50 +00:00
parent 05beb4d5d0
commit 074b08841e
1 changed files with 10 additions and 13 deletions

View File

@ -69,18 +69,12 @@ class Mutex
*/ */
pthread_mutex_t * mutex; pthread_mutex_t * mutex;
/**
* The error code returned by tryLockMutex().
*/
int mutexError;
public: public:
/** /**
* Default constructor. * Default constructor.
*/ */
Mutex(void) throw () Mutex(void) throw ()
: mutexError(0)
{ {
mutex = new pthread_mutex_t; mutex = new pthread_mutex_t;
pthread_mutex_init(mutex, NULL); pthread_mutex_init(mutex, NULL);
@ -123,19 +117,22 @@ class Mutex
bool bool
tryLock(void) throw () tryLock(void) throw ()
{ {
mutexError = pthread_mutex_trylock(mutex); return (pthread_mutex_trylock(mutex) == 0);
return (mutexError == 0);
} }
/** /**
* Get the error code (if any) after a call to tryLockMutex(). * Try to lock a mutex.
* If the mutex is already locked, it returns false.
* *
* @return the error code; 0 for no error. *` @param errorCode return parameter for the error code;
* 0 for no error.
* @return true if the mutex was successfully locked; false otherwise.
*/ */
int bool
getMutexError(void) throw () tryLock(int & errorCode) throw ()
{ {
return mutexError; errorCode = pthread_mutex_trylock(mutex);
return (errorCode == 0);
} }
}; };