[Libav-user] Best way to create a fractional ms timer?

John Orr john.orr at scala.com
Sun Mar 31 20:31:09 CEST 2013

On 3/31/2013 7:42 AM, Александр Рухлов wrote:
> timeBeginPeriod() + timeGetTime() +...+ Sleep() + timeEndPeriod()
> http://msdn.microsoft.com/ru-RU/library/windows/desktop/dd757624%28v=vs.85%29.aspx

Note that you should not set timeBeginPeriod frequently, one per process 
is best if you can manage it. In the past I've seen PCs+BIOS lose a 
little bit of time off of their real time clock when calling 
timeBeginPeriod() so they get way out of sync if you call then frequently.

Here are some still relevant links to timing issues in Windows:



On top of the timing function calls, you may need to raise the priority 
of the thread that calls Sleep() to something time critical. It was 
pretty much a necessity with Windows XP and earlier. If you didn't, 
there is no guarantee that the thread that called Sleep() would regain 
CPU immediately after the sleep period was finished. Once a 
non-time-critical thread yields the CPU, the OS has no special reason to 
reschedule it ahead of any other thread. When the Sleep period finished, 
the sleeping thread has to get back in line with all the other threads. 
The rules are different for time critical threads, the can get the CPU 
back ahead of the rest of the pack. The Windows thread scheduler saw 
some changes for Vista and later, I'm not as familiar with the behavior 
there, so I don't know if Microsoft improved that situation.


More information about the Libav-user mailing list