Listing 3 Demonstrating the use of the locks monitor
#include "locksWrapperMacros.h" // globals HANDLE g_hResource[2] = { NULL, NULL }; void f2() { WaitForSingleObject(g_hResource[1], INFINITE); Sleep(100); WaitForSingleObject(g_hResource[0], INFINITE); ReleaseMutex(g_hResource[1]); ReleaseMutex(g_hResource[0]); } void f1() { WaitForSingleObject(g_hResource[0], INFINITE); Sleep(100); WaitForSingleObject(g_hResource[1], INFINITE); ReleaseMutex(g_hResource[0]); ReleaseMutex(g_hResource[1]); } DWORD WINAPI Thread_1(LPVOID pParam) { f1(); return 0; }; DWORD WINAPI Thread_2(LPVOID pParam) { f2(); return 0; }; void main() { HRESULT hr = S_FALSE; // get ptr to the only instance of CLocksMonitorMgr CLocksMonitorMgr* pMgr = CLocksMonitorMgr::GetInstance(); HANDLE hThread[2] = { NULL, NULL }; DWORD dwThreadId = 0; // start locks monitor via its manager pMgr->Start(); for (int i = 0; i < 2; i++) g_hResource[i] = CreateMutex(NULL, FALSE, NULL); hThread[0] = CreateThread(NULL, 0, Thread_1, NULL, 0, &dwThreadId); hThread[1] = CreateThread(NULL, 0, Thread_2, NULL, 0, &dwThreadId); WaitForMultipleObjects(2,hThread,TRUE,INFINITE); // stop locks monitor via its manager pMgr->Stop(); }