Guitar
mutex.h
Go to the documentation of this file.
1 
2 #ifndef __MUTEX_H
3 #define __MUTEX_H
4 
5 
6 #ifdef WIN32
7 
8 #include <windows.h>
9 class Mutex {
10 public:
11  HANDLE _handle;
12  Mutex()
13  {
14  _handle = CreateMutex(0, FALSE, 0);
15  }
16  ~Mutex()
17  {
18  CloseHandle(_handle);
19  }
20  void lock()
21  {
22  WaitForSingleObject(_handle, INFINITE);
23  }
24  void unlock()
25  {
26  ReleaseMutex(_handle);
27  }
28 };
29 
30 #else
31 
32 #include <pthread.h>
33 class Mutex {
34 public:
35  pthread_mutex_t _handle;
37  {
38  pthread_mutex_init(&_handle, NULL);
39  }
41  {
42  pthread_mutex_destroy(&_handle);
43  }
44  void lock()
45  {
46  pthread_mutex_lock(&_handle);
47  }
48  void unlock()
49  {
50  pthread_mutex_unlock(&_handle);
51  }
52 };
53 
54 #endif
55 
56 
57 class AutoLock {
59 public:
60  AutoLock(Mutex *mutex)
61  {
62  _mutex = mutex;
63  _mutex->lock();
64  }
66  {
67  _mutex->unlock();
68  }
69 };
70 
71 
72 
73 #endif
74 
AutoLock::_mutex
Mutex * _mutex
Definition: mutex.h:58
Mutex::lock
void lock()
Definition: mutex.h:44
Mutex::~Mutex
~Mutex()
Definition: mutex.h:40
AutoLock
Definition: mutex.h:57
AutoLock::~AutoLock
~AutoLock()
Definition: mutex.h:65
Mutex
Definition: mutex.h:33
AutoLock::AutoLock
AutoLock(Mutex *mutex)
Definition: mutex.h:60
Mutex::_handle
pthread_mutex_t _handle
Definition: mutex.h:35
Mutex::Mutex
Mutex()
Definition: mutex.h:36
Mutex::unlock
void unlock()
Definition: mutex.h:48