Guest User

Untitled

a guest
Jun 20th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. HANDLE mutex = CreateMutex(NULL, FALSE, NULL);
  2. CRITICAL_SECTION critSec;
  3. InitializeCriticalSection(&critSec);
  4.  
  5. LARGE_INTEGER freq;
  6. QueryPerformanceFrequency(&freq);
  7. LARGE_INTEGER start, end;
  8.  
  9. // Force code into memory, so we don't see any effects of paging.
  10. EnterCriticalSection(&critSec);
  11. LeaveCriticalSection(&critSec);
  12. QueryPerformanceCounter(&start);
  13. for (int i = 0; i < 1000000; i++)
  14. {
  15. EnterCriticalSection(&critSec);
  16. LeaveCriticalSection(&critSec);
  17. }
  18.  
  19. QueryPerformanceCounter(&end);
  20.  
  21. int totalTimeCS = (int)((end.QuadPart - start.QuadPart) * 1000 / freq.QuadPart);
  22.  
  23. // Force code into memory, so we don't see any effects of paging.
  24. WaitForSingleObject(mutex, INFINITE);
  25. ReleaseMutex(mutex);
  26.  
  27. QueryPerformanceCounter(&start);
  28. for (int i = 0; i < 1000000; i++)
  29. {
  30. WaitForSingleObject(mutex, INFINITE);
  31. ReleaseMutex(mutex);
  32. }
  33.  
  34. QueryPerformanceCounter(&end);
  35.  
  36. int totalTime = (int)((end.QuadPart - start.QuadPart) * 1000 / freq.QuadPart);
  37.  
  38. printf("Mutex: %d CritSec: %dn", totalTime, totalTimeCS);
  39.  
  40. StartCriticalSection();
  41. DoSomethingImportant();
  42. DoSomeOtherImportantThing();
  43. EndCriticalSection();
Add Comment
Please, Sign In to add comment