Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- HANDLE mutex = CreateMutex(NULL, FALSE, NULL);
- CRITICAL_SECTION critSec;
- InitializeCriticalSection(&critSec);
- LARGE_INTEGER freq;
- QueryPerformanceFrequency(&freq);
- LARGE_INTEGER start, end;
- // Force code into memory, so we don't see any effects of paging.
- EnterCriticalSection(&critSec);
- LeaveCriticalSection(&critSec);
- QueryPerformanceCounter(&start);
- for (int i = 0; i < 1000000; i++)
- {
- EnterCriticalSection(&critSec);
- LeaveCriticalSection(&critSec);
- }
- QueryPerformanceCounter(&end);
- int totalTimeCS = (int)((end.QuadPart - start.QuadPart) * 1000 / freq.QuadPart);
- // Force code into memory, so we don't see any effects of paging.
- WaitForSingleObject(mutex, INFINITE);
- ReleaseMutex(mutex);
- QueryPerformanceCounter(&start);
- for (int i = 0; i < 1000000; i++)
- {
- WaitForSingleObject(mutex, INFINITE);
- ReleaseMutex(mutex);
- }
- QueryPerformanceCounter(&end);
- int totalTime = (int)((end.QuadPart - start.QuadPart) * 1000 / freq.QuadPart);
- printf("Mutex: %d CritSec: %dn", totalTime, totalTimeCS);
- StartCriticalSection();
- DoSomethingImportant();
- DoSomeOtherImportantThing();
- EndCriticalSection();
Add Comment
Please, Sign In to add comment