-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mutex.h
61 lines (55 loc) · 1.12 KB
/
Mutex.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <windows.h>
class Mutex {
private:
HANDLE handle_;
public:
Mutex(const char* name = "__my_mutex__")
{
handle_ = CreateMutex(NULL, FALSE, (LPCWSTR)(name));
//handle_ = OpenMutex(MUTEX_ALL_ACCESS, FALSE, TEXT("__info_Mutex__"));
if (handle_ == NULL)
int tja = 3;
}
/*Mutex(const char* name){
handle_ = CreateMutex(nullptr, false, (LPWSTR)(name));
}
*/
~Mutex()
{
ReleaseMutex(handle_);
}
bool TryLock()
{
DWORD tRet = WaitForSingleObject(handle_, 0);
if (tRet == WAIT_FAILED)
{
return true;
}
else if (tRet == WAIT_OBJECT_0)
{
return true;
}
else if (tRet == WAIT_TIMEOUT)
{
return true;
}
else if (tRet == WAIT_ABANDONED)
{
return true;
}
}
bool Lock(DWORD milliseconds = 10000000000000) //inte bra att låsa forever ifall programmet dör, då unlockas den aldrig
{
DWORD check;
check = WaitForSingleObject(handle_, milliseconds);
//Sleep(milliseconds); //check returneras direkt så vänta ut skiten oxå??
if (check == WAIT_ABANDONED) {
return false; //didnt get mutex
}
return true; //got mutex
}
void Unlock()
{
ReleaseMutex(handle_);
}
};