SHARE
TWEET

windows timers

a guest Jun 6th, 2015 215 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <uuids.h>
  4. #include <dsound.h>
  5. #include <math.h>
  6. #include <strmif.h>
  7.  
  8. // compile as
  9. // winegcc -g timers.cpp -lwinmm -lole32 -lstrmiids
  10.  
  11.  void wait(HANDLE object, int milliseconds)
  12.  {
  13.   LARGE_INTEGER frequency;
  14.   QueryPerformanceFrequency(&frequency);
  15.   double f = frequency.QuadPart;
  16.   double period = 1000.0 / f; // in ms
  17.  
  18.   LONGLONG previous = 0L;
  19.   double sum = 0.;
  20.   double sum2 = 0.;
  21.  
  22.   int k = 0;
  23.   do
  24.   {
  25.     WaitForSingleObject(object, INFINITE);
  26.     ++k;
  27.  
  28.     LARGE_INTEGER t1;
  29.     QueryPerformanceCounter(&t1);
  30.  
  31.     if (k > 1)
  32.     {
  33.       double thisGap = double(t1.QuadPart - previous) * period;
  34.       sum += thisGap;
  35.       sum2 += thisGap * thisGap;
  36.     }
  37.     previous = t1.QuadPart;
  38.   }
  39.   while (k < 10 * 1000 / milliseconds); // number of waits for 10 seconds
  40.  
  41.   double average = double(sum) / double(k - 17);
  42.   double average2 = double(sum2) / double(k - 1);
  43.   double stddev = sqrt(average2 - average * average);
  44.  
  45.   printf("k: %d\n", k);
  46.   printf("average: %g\n", average);
  47.   printf("stddev: %g\n", stddev);
  48. }
  49.  
  50. void main1(int milliseconds)
  51. {
  52.   HANDLE hTimer;
  53.   LARGE_INTEGER liDueTime = { 0 };
  54.  
  55.   printf("CreateWaitableTimer\n");
  56.  
  57.   hTimer = CreateWaitableTimer (NULL, FALSE, NULL);
  58.   SetWaitableTimer(hTimer, &liDueTime, milliseconds, NULL, NULL, 0);
  59.  
  60.   wait(hTimer, milliseconds);
  61. }
  62.  
  63. void main2(int milliseconds)
  64. {
  65.   HANDLE g_hSemaphore;
  66.   IReferenceClock *g_RefClock;
  67.   DWORD_PTR g_dwAdviseToken;
  68.  
  69.   printf("IReferenceClock\n");
  70.  
  71.   g_hSemaphore = CreateSemaphore(NULL, 0, 1, NULL);
  72.   CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
  73.   CoCreateInstance(CLSID_SystemClock, NULL, CLSCTX_INPROC, IID_IReferenceClock, (LPVOID*)&g_RefClock);
  74.  
  75.   REFERENCE_TIME rtPeriod = (REFERENCE_TIME)(10000 * milliseconds);
  76.   REFERENCE_TIME rtNow;
  77.  
  78.   g_RefClock->GetTime(&rtNow);
  79.  
  80.   // VS Build
  81. #ifdef _MSC_VER
  82.   g_RefClock->AdvisePeriodic(rtNow + rtPeriod, rtPeriod, g_hSemaphore, &g_dwAdviseToken);
  83. #else
  84.   g_RefClock->AdvisePeriodic(rtNow + rtPeriod, rtPeriod, (HSEMAPHORE)g_hSemaphore, &g_dwAdviseToken);
  85. #endif
  86.  
  87.   wait(g_hSemaphore, milliseconds);
  88. }
  89.  
  90. VOID CALLBACK TimerRoutine(PVOID lpParam, BOOLEAN TimerOrWaitFired)
  91. {
  92.   HANDLE & gDoneEvent = *(HANDLE*)lpParam;
  93.   SetEvent(gDoneEvent);
  94. }
  95.  
  96. void main3(int milliseconds)
  97. {
  98.   HANDLE hTimer;
  99.   HANDLE hTimerQueue;
  100.   HANDLE gDoneEvent;
  101.  
  102.   printf("CreateTimerQueueTimer\n");
  103.  
  104.   gDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  105.   hTimerQueue = CreateTimerQueue();
  106.   CreateTimerQueueTimer(&hTimer, hTimerQueue, (WAITORTIMERCALLBACK)TimerRoutine, &gDoneEvent, 0, milliseconds, 0);
  107.  
  108.   wait(gDoneEvent, milliseconds);
  109. }
  110.  
  111. void main4(int milliseconds)
  112. {
  113.   HANDLE gDoneEvent;
  114.  
  115.   printf("timeSetEvent\n");
  116.  
  117.   gDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  118.  
  119.   int resolution = 0;
  120.   timeSetEvent(milliseconds, resolution, (LPTIMECALLBACK)gDoneEvent, 0, TIME_PERIODIC | TIME_CALLBACK_EVENT_SET);
  121.  
  122.   wait(gDoneEvent, milliseconds);
  123. }
  124.  
  125. int main(int argc, char * argv[])
  126. {
  127.   if (argc == 3)
  128.   {
  129.     const char * mstr = argv[1];
  130.     int milliseconds = atoi(mstr);
  131.    
  132.     printf("Waiting every %d milliseconds for 10 seconds...\n", milliseconds);
  133.    
  134.     char c = argv[2][0];
  135.     switch (c)
  136.     {
  137.     case '1': main1(milliseconds); break;
  138.     case '2': main2(milliseconds); break;
  139.     case '3': main3(milliseconds); break;
  140.     case '4': main4(milliseconds); break;
  141.     }
  142.   }
  143.   return 0;
  144. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top