// to do: build the code and execute the binary twice at the same time!
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE hMutex;
DWORD dwWaitResult;
for( int i=0 ; i<3 ; i++ )
{
// if the mutex with the same name exists, it opens the mutex with MUTEX_ALL_ACCESS right.
hMutex = CreateMutex(NULL, FALSE, _T("XXXXXXX_uid:0a0b6203-cd11-4b50-877b-3965da7275cc"));
// waiting for other thread release the mutex
// then this thread will get the mutex
_tprintf( _T("Waiting for the mutex... ") );
dwWaitResult = WaitForSingleObject(hMutex, INFINITE);
switch(dwWaitResult)
{
case WAIT_OBJECT_0:
// This thread got a mutex!
_tprintf(_T ("thread got mutex ! \n") );
break;
case WAIT_TIMEOUT:
// A time out occurs...
_tprintf(_T ("timer expired ! \n") );
break;
case WAIT_ABANDONED:
// Another thread may not release the mutex before it terminated.
// The mutex has been abandoned.... how poor....
_tprintf(_T ("abandoned ! \n") );
break;
}
_tprintf( _T("Thread Running (%d) ! \n"), i+1 );
for( int j=0 ; j<10 ; j++)
{
_puttch(_T('.'));
Sleep(100);
}
_puttch(_T('\n'));
// should release the mutex
_tprintf( _T("One loop ended, the mutex will be released! \n\n") );
ReleaseMutex(hMutex);
}
_tprintf( _T("Test has terminated.\nPress any key to continue... \n") );
_gettch();
return 0;
}