Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #if defined OS_WINDOWS
- struct Lock //thanks to 'doublep' from StackOverflow for this RAII solution
- {
- MUTEX& mutex;
- bool locked;
- Lock (MUTEX& mutex)
- : mutex (mutex),
- locked (false)
- { }
- ~Lock ()
- { release (); }
- bool acquire (int timeout = -1)
- {
- if (!locked && WaitForSingleObject (mutex, timeout) == WAIT_OBJECT_0)
- locked = true;
- return locked;
- }
- int release ()
- {
- if (locked)
- locked = (ReleaseMutex (mutex) == 1);
- return !locked;
- }
- };
- MUTEX mutex_q = NULL;
- MUTEX mutex_p = NULL;
- #else
- struct Lock //and here is my little edit for linux
- {
- MUTEX& mutex;
- bool locked;
- Lock (MUTEX& mutex)
- : mutex (mutex),
- locked (false)
- { }
- ~Lock ()
- { release (); }
- bool acquire (int timeout = -1)
- {
- if (!locked && pthread_mutex_lock (&mutex) == 0)
- locked = true;
- return locked;
- }
- int release ()
- {
- if (locked)
- locked = (pthread_mutex_unlock (&mutex) == 1);
- return !locked;
- }
- };
- MUTEX mutex_q = PTHREAD_MUTEX_INITIALIZER;
- MUTEX mutex_p = PTHREAD_MUTEX_INITIALIZER;
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement