Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

simple Mutex class

  • Loading branch information...
commit ea3f129c446e6aed648b657b65f428174e079149 1 parent e7f3b1d
@albertz authored
Showing with 37 additions and 0 deletions.
  1. +37 −0 Mutex.h
View
37 Mutex.h
@@ -0,0 +1,37 @@
+/* Mutex class - small wrapper around pthread
+ * by Albert Zeyer, 2011
+ * code under LGPL
+ */
+
+#ifndef __AZ__MUTEX_H__
+#define __AZ__MUTEX_H__
+
+#include "Utils.h"
+#include <pthread.h>
+
+struct Mutex {
+ pthread_mutex_t m;
+ Mutex() { pthread_mutex_init(&m, NULL); }
+ Mutex(const Mutex&) { pthread_mutex_init(&m, NULL); } // ignore copy constr
+ Mutex& operator=(const Mutex&) {} // ignore assign
+ ~Mutex() { pthread_mutex_destroy(&m); }
+ void lock() { pthread_mutex_lock(&m); }
+ bool trylock() { if(pthread_mutex_trylock(&m) == 0) return true; return false; }
+ void unlock() { pthread_mutex_unlock(&m); }
+};
+
+struct ScopedLock : DontCopyTag {
+ Mutex& mutex;
+ ScopedLock(Mutex& m) : mutex(m) { mutex.lock(); }
+ ~ScopedLock() { mutex.unlock(); }
+};
+
+struct ScopedTryLock : DontCopyTag {
+ Mutex& mutex;
+ bool islocked;
+ ScopedTryLock(Mutex& m) : mutex(m), islocked(false) { islocked = mutex.trylock(); }
+ ~ScopedTryLock() { if(islocked) { mutex.unlock(); islocked = false; } }
+ operator bool() const { return islocked; }
+};
+
+#endif
Please sign in to comment.
Something went wrong with that request. Please try again.