High-resolution thread sleep

Ivan Trombley itrombley at dot-borg.org
Thu Dec 14 23:45:18 UTC 2017


On Thursday, 14 December 2017 at 21:47:05 UTC, Rene Zwanenburg 
wrote:
> On Thursday, 14 December 2017 at 21:11:34 UTC, Ivan Trombley 
> wrote:
>> I need to be able to put a thread to sleep for some amount of 
>> time. I was looking at using Thread.sleep but it appears that 
>> on Windows, it's limited to millisecond resolution. Is there a 
>> way to do this in a cross-platform way that is higher 
>> resolution?
>
> Sleeping for very short periods is usually a bad idea for 
> various reasons. What do you need it for?
>
> If you really need this, go for a loop with a high resolution 
> timer.

Something along the lines of this:

while (render)
{
   immutable auto startTime = MonoTime.currTime;

   // Render the frame...

   immutable auto remain = m_frameDuration - (startTime - 
MonoTime.currTime);
   if (remain > Duration.zero)
     Thread.sleep(remain);
}



More information about the Digitalmars-d-learn mailing list