Thread.sleep (DMD 2.020)

John C johnch_atms at hotmail.com
Sat Nov 1 10:25:05 PDT 2008


Sean Kelly Wrote:

> John C wrote:
> > Sean Kelly Wrote:
> > 
> >> Sean Kelly wrote:
> >>> John C wrote:
> >>>> I think Thread.sleep, which was introduced in core.thread in 2.020, is 
> >>>> broken on Win32. No matter what value I pass to the function, it 
> >>>> merely sleeps for 1 millisecond. Looks like the problem is here:
> >>>>
> >>>>   period = period < TICKS_PER_MILLI ?     1 : // 
> >>>> <------------------------------------------------- BUG?
> >>>>     period / TICKS_PER_MILLI;
> >>>>
> >>>> Should it be testing if period is greater than MAX_SLEEP_MILLS, rather 
> >>>> than less than TICKS_PER_MILLI?
> >>> This test was meant to make the min sleep time 1 millisecond, but I 
> >>> think I'm going to remove it.
> >> Oh, I should mention that Thread.sleep() actually works correctly, even 
> >> with this check in place.  The sleep parameter uses a 100ns resolution, 
> >> so to sleep for one millisecond you must use 10_000, while to sleep for 
> >> one second it's 10_000_000.  This isn't the same as Windows, which uses 
> >> a millisecond resolution.  I've tested this routine on Windows, however, 
> >> and have verified that it works correctly.  The docs are wrong however, 
> >> as they say 500 = one millisecond.  I'll fix that.
> > 
> > Now you tell me :-)
> > 
> > Win32's Sleep(50) seems to be the same as Thread.sleep(500_000). Is that right?
> 
> Yup.  There should probably be some sort of TimeSpan struct to help 
> prevent these mistakes.
> 
> 
> Aean

That's why I wrote Mango's TimeSpan way back when...



More information about the Digitalmars-d mailing list