00001 #ifndef WIN32_MUTEX_H
00002 #define WIN32_MUTEX_H
00003
00004 #include <windows.h>
00005
00006
00007
00008
00009
00010
00011 class Mutex
00012 {
00013 public:
00014
00015 Mutex (void)
00016 {
00017 mutex = new CRITICAL_SECTION;
00018 InitializeCriticalSection (mutex);
00019 refs = new int;
00020 *refs = 1;
00021 }
00022
00023
00024 Mutex (const Mutex& m)
00025 {
00026 mutex = m.mutex;
00027 refs = m.refs;
00028 (*refs)++;
00029 }
00030
00031
00032 Mutex& operator = (const Mutex& m)
00033 {
00034 (*refs)--;
00035 if (*refs == 0)
00036 {
00037 delete refs;
00038 DeleteCriticalSection (mutex);
00039 delete mutex;
00040 }
00041
00042 mutex = m.mutex;
00043 refs = m.refs;
00044 (*refs)++;
00045 return *(this);
00046 }
00047
00048
00049 void Lock (void)
00050 {
00051 EnterCriticalSection (mutex);
00052 }
00053
00054
00055 void Unlock (void)
00056 {
00057 LeaveCriticalSection (mutex);
00058 }
00059
00060
00061 ~Mutex (void)
00062 {
00063 (*refs)--;
00064 if (*refs == 0)
00065 {
00066 delete refs;
00067 DeleteCriticalSection (mutex);
00068 delete mutex;
00069 }
00070 }
00071
00072 private:
00073
00074 int *refs;
00075
00076
00077 CRITICAL_SECTION *mutex;
00078 };
00079
00080 #endif