Created
February 7, 2017 08:13
-
-
Save tkojitu/ada6a7ed7bf45f058a9b510c297785d3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "stdafx.h" | |
#include <Windows.h> | |
static HANDLE s_mutex; | |
bool lock() { | |
s_mutex = CreateMutex(NULL, TRUE, L"TextMutex"); | |
if (!s_mutex) { | |
fprintf(stderr, "CreateMutex failed %d\n", GetLastError()); | |
return false; | |
} | |
if (GetLastError() == ERROR_ALREADY_EXISTS) { | |
unsigned long ret = WaitForSingleObject(s_mutex, 1000 * 10); | |
if (ret != WAIT_OBJECT_0) { | |
fprintf(stderr, "WaitForSingleObject failed %d, %d\n", ret, GetLastError()); | |
return false; | |
} | |
} | |
return true; | |
} | |
void unlock() { | |
if (!s_mutex) { | |
return; | |
} | |
if (!ReleaseMutex(s_mutex)) { | |
fprintf(stderr, "ReleaseMutex failed %d\n", GetLastError()); | |
} | |
} | |
int _tmain(int argc, _TCHAR* argv[]) { | |
if (lock()) { | |
fprintf(stderr, "sleeping...\n"); | |
Sleep(1000 * 10); | |
fprintf(stderr, "wake up\n"); | |
} | |
unlock(); | |
CloseHandle(s_mutex); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "stdafx.h" | |
#include <Windows.h> | |
static HANDLE s_mutex; | |
static bool DoLong() { | |
fprintf(stderr, "DoLong\n"); | |
s_mutex = CreateMutex(NULL, FALSE, L"MutexDemo"); | |
if (!s_mutex) { | |
fprintf(stderr, "DoLong: CreateMutex failed %d\n", GetLastError()); | |
return false; | |
} | |
DWORD ret = WaitForSingleObject(s_mutex, INFINITE); | |
switch (ret) { | |
case WAIT_ABANDONED: | |
fprintf(stderr, "DoLong: WaitForSingleObject failed WAIT_ABANDONED\n"); | |
return false; | |
case WAIT_TIMEOUT: | |
fprintf(stderr, "DoLong: WaitForSingleObject failed WAIT_TIMEOUT\n"); | |
return false; | |
case WAIT_FAILED: | |
fprintf(stderr, "DoLong: WaitForSingleObject failed %d\n", GetLastError()); | |
return false; | |
case WAIT_OBJECT_0: | |
default: | |
fprintf(stderr, "sleeping...\n"); | |
Sleep(1000 * 10); | |
return true; | |
} | |
} | |
static bool DoShort() { | |
fprintf(stderr, "DoShort\n"); | |
s_mutex = CreateMutex(NULL, FALSE, L"MutexDemo"); | |
if (!s_mutex) { | |
fprintf(stderr, "DoLong: CreateMutex failed %d\n", GetLastError()); | |
return false; | |
} | |
DWORD ret = WaitForSingleObject(s_mutex, 1000 * 5); | |
switch (ret) { | |
case WAIT_ABANDONED: | |
fprintf(stderr, "DoLong: WaitForSingleObject failed WAIT_ABANDONED\n"); | |
return false; | |
case WAIT_TIMEOUT: | |
fprintf(stderr, "DoLong: WaitForSingleObject failed WAIT_TIMEOUT\n"); | |
return false; | |
case WAIT_FAILED: | |
fprintf(stderr, "DoLong: WaitForSingleObject failed %d\n", GetLastError()); | |
return false; | |
case WAIT_OBJECT_0: | |
default: | |
return true; | |
} | |
} | |
static bool DoDouble() { | |
fprintf(stderr, "DoDouble\n"); | |
s_mutex = CreateMutex(NULL, FALSE, L"MutexDemo"); | |
if (!s_mutex) { | |
fprintf(stderr, "DoLong: CreateMutex failed %d\n", GetLastError()); | |
return false; | |
} | |
if (GetLastError() == ERROR_ALREADY_EXISTS) { | |
fprintf(stderr, "CreateMutex failed ERROR_ALREADY_EXISTS\n"); | |
return false; | |
} | |
DWORD ret = WaitForSingleObject(s_mutex, 1000 * 5); | |
switch (ret) { | |
case WAIT_ABANDONED: | |
fprintf(stderr, "DoLong: WaitForSingleObject failed WAIT_ABANDONED\n"); | |
return false; | |
case WAIT_TIMEOUT: | |
fprintf(stderr, "DoLong: WaitForSingleObject failed WAIT_TIMEOUT\n"); | |
return false; | |
case WAIT_FAILED: | |
fprintf(stderr, "DoLong: WaitForSingleObject failed %d\n", GetLastError()); | |
return false; | |
case WAIT_OBJECT_0: | |
default: | |
return true; | |
} | |
} | |
int _tmain(int argc, _TCHAR* argv[]) { | |
switch (argv[1][0]) { | |
case '0': | |
if (!DoLong()) { | |
return EXIT_FAILURE; | |
} | |
break; | |
case '1': | |
if (!DoShort()) { | |
return EXIT_FAILURE; | |
} | |
break; | |
case '2': | |
if (!DoDouble()) { | |
return EXIT_FAILURE; | |
} | |
break; | |
default: | |
break; | |
} | |
if (s_mutex) { | |
if (!ReleaseMutex(s_mutex)) { | |
fprintf(stderr, "ReleaseMutex failed %d\n", GetLastError()); | |
} | |
CloseHandle(s_mutex); | |
} | |
fprintf(stderr, "ReleaseMutex\n"); | |
Sleep(1000 * 5); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment