Please see the code below:
#include <windows.h>
int main(int argc, char* argv[])
{
HANDLE _mutex = ::CreateMutex(NULL, FALSE, "abc");
if (!_mutex)
throw std::runtime_error("CreateMutex failed");
if (::WaitForSingleObject(_mutex, INFINITE) != WAIT_OBJECT_0)
throw std::runtime_error("WaitForSingleObject failed");
printf("Must lock here\n");
if (::WaitForSingleObject(_mutex, INFINITE) != WAIT_OBJECT_0)
throw std::runtime_error("WaitForSingleObject failed");
printf("Why come here????\n");
return 0;
}
I don't know why console print out:
Must lock here
Why come here???
Does mutex not work? I want the result only show
Must lock here
And blocking after print the text above.