[Solved(with a workaround)]Time::waitForMillisecondCounter () is very inaccurate on Windows

Hello JUCErs,

I have the following problem:

  • I have a high-priority thread (level 8 - 10) which is supposed to run a specific job every 10 milliseconds.
  • The job doesn’t take that long (under 1 millisecond on both Windows and Mac OS X);
  • The thread is being paused between runs for 10 milliseconds, using the Time::waitForMillisecondCounter () function;
  • On OS X this is very accurate (deviation is about .04 of a millisecond - just about enough to account for the time spent on the task it executes on this interval) and it runs ~100 times per second;
  • [The Problem]: On Windows this same pause takes on average 430 milliseconds which allows the thread to run only ~2 times per second.

Here is the relevant code to reproduce the issue with JUCE 4.3.0 (latest official):

while (shouldRun.get () > 0) // atomic

    const double currentTime = Time::getMillisecondCounterHiRes (),
                 targetTimeToWaitUntil = currentTime + interval; // interval is 10 milliseconds

    Time::waitForMillisecondCounter (targetTimeToWaitUntil);

I am testing on Windows 8.1 64bit, with a 64bit VST plugin running within the Plugin Host example of the JUCE library.

How do I make this consistently work on both OS X and Windows (keeping the accuracy of OS X, of course)?

Using the Thread::wait () method works a tiny bit worse on OS X and is consistent with Windows.

@otristan Thanks for the help!

Please post the help so others may benefit

I did - I only replaced Time::waitForMillisecondCounter () with the wait () method of the thread. That’s it! :slight_smile: