digitalmars.D - Thread.sleep (DMD 2.020)
- John C (7/7) Nov 01 2008 I think Thread.sleep, which was introduced in core.thread in 2.020, is b...
- Sean Kelly (5/13) Nov 01 2008 This test was meant to make the min sleep time 1 millisecond, but I
- John C (2/8) Nov 01 2008 Also, any reason why priority is only available once the Thread's handle...
- Sean Kelly (3/9) Nov 01 2008 Do you mean as opposed to in its ctor?
- Sean Kelly (9/23) Nov 01 2008 Oh, I should mention that Thread.sleep() actually works correctly, even
- John C (3/29) Nov 01 2008 Now you tell me :-)
- Sean Kelly (4/31) Nov 01 2008 Yup. There should probably be some sort of TimeSpan struct to help
- John C (2/36) Nov 01 2008 That's why I wrote Mango's TimeSpan way back when...
- Christopher Wright (7/18) Nov 01 2008 Unfortunately, at the moment, that would mean putting TimeSpan into
- Steven Schveighoffer (9/28) Nov 02 2008 Tango uses TimeSpan in most places (including Socket) except for core, w...
- Sean Kelly (9/37) Nov 02 2008 Walter pointed out to me that by using floating point in a runtime
- torhu (5/11) Nov 02 2008 I assume people won't need a higher resolution than milliseconds that
- Sean Kelly (14/27) Nov 02 2008 While Windows uses milliseconds, *nix uses either micrososeconds
- John C (4/34) Nov 02 2008 It is:
- John C (2/14) Nov 01 2008 Not really - I mean that you should be able to set a Thread object's pri...
- Sean Kelly (12/24) Nov 01 2008 Thread.start() mostly exists to ensure that classes derived from Thread
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? Should I file a bug report? John
Nov 01 2008
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.Should I file a bug report?Don't bother. But thanks for the report. Sean
Nov 01 2008
Sean Kelly Wrote:Also, any reason why priority is only available once the Thread's handle has been created - ie, after it's been started?Should I file a bug report?Don't bother. But thanks for the report. Sean
Nov 01 2008
John C wrote:Sean Kelly Wrote:Do you mean as opposed to in its ctor? SeanAlso, any reason why priority is only available once the Thread's handle has been created - ie, after it's been started?Should I file a bug report?Don't bother. But thanks for the report.
Nov 01 2008
Sean Kelly wrote:John C wrote: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. SeanI 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.
Nov 01 2008
Sean Kelly Wrote:Sean Kelly wrote:Now you tell me :-) Win32's Sleep(50) seems to be the same as Thread.sleep(500_000). Is that right?John C wrote: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. SeanI 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.
Nov 01 2008
John C wrote:Sean Kelly Wrote:Yup. There should probably be some sort of TimeSpan struct to help prevent these mistakes. AeanSean Kelly wrote:Now you tell me :-) Win32's Sleep(50) seems to be the same as Thread.sleep(500_000). Is that right?John C wrote: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.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.
Nov 01 2008
Sean Kelly Wrote:John C wrote:That's why I wrote Mango's TimeSpan way back when...Sean Kelly Wrote:Yup. There should probably be some sort of TimeSpan struct to help prevent these mistakes. AeanSean Kelly wrote:Now you tell me :-) Win32's Sleep(50) seems to be the same as Thread.sleep(500_000). Is that right?John C wrote: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.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.
Nov 01 2008
John C wrote:Sean Kelly Wrote:Unfortunately, at the moment, that would mean putting TimeSpan into druntime (dcore?), which probably isn't going to happen. I hope that TimeSpan can make its way into a core part of Tango eventually. Socket, for instance, uses struct timeval directly -- if I recall correctly, client code has to construct timevals, and there's no constructor provided.John C wrote:That's why I wrote Mango's TimeSpan way back when...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
Nov 01 2008
"Christopher Wright" wroteJohn C wrote:Tango uses TimeSpan in most places (including Socket) except for core, which can't depend on other libs (TimeSpan resides in tango.time.Time). In places where TimeSpan cannot be used, or where it was deemed undesirable, a double representing seconds is used. My original intention for TimeSpan was for it to be in core for this reason, but I was overruled. If I had it my way, TimeSpan would be the only representation of time in Tango. -SteveSean Kelly Wrote:Unfortunately, at the moment, that would mean putting TimeSpan into druntime (dcore?), which probably isn't going to happen. I hope that TimeSpan can make its way into a core part of Tango eventually. Socket, for instance, uses struct timeval directly -- if I recall correctly, client code has to construct timevals, and there's no constructor provided.John C wrote:That's why I wrote Mango's TimeSpan way back when...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
Nov 02 2008
Steven Schveighoffer wrote:"Christopher Wright" wroteWalter pointed out to me that by using floating point in a runtime module that's always linked (ie. Thread), the floating point support code will be linked into every app as well. This is why I changed the sleep routine in druntime.John C wrote:Tango uses TimeSpan in most places (including Socket) except for core, which can't depend on other libs (TimeSpan resides in tango.time.Time). In places where TimeSpan cannot be used, or where it was deemed undesirable, a double representing seconds is used.Sean Kelly Wrote:Unfortunately, at the moment, that would mean putting TimeSpan into druntime (dcore?), which probably isn't going to happen. I hope that TimeSpan can make its way into a core part of Tango eventually. Socket, for instance, uses struct timeval directly -- if I recall correctly, client code has to construct timevals, and there's no constructor provided.John C wrote:That's why I wrote Mango's TimeSpan way back when...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. AeanMy original intention for TimeSpan was for it to be in core for this reason, but I was overruled. If I had it my way, TimeSpan would be the only representation of time in Tango.Me too. It's silly that the core modules use a different representation for time than the other modules in Tango simply because the time structs were deemed too non-essential to place in the core. Sean
Nov 02 2008
Sean Kelly wrote:John C wrote:...I assume people won't need a higher resolution than milliseconds that often. Could Thread.sleep be changed into something like this? Thread.sleep(long millis, long hundredsOfNanos=0);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.
Nov 02 2008
torhu wrote:Sean Kelly wrote:While Windows uses milliseconds, *nix uses either micrososeconds (usleep) or a combination of seconds and nanoseconds (nanosleep), so while using milliseconds may be more natural for Windows users it's liable to confuse others. I suppose what I'm getting at is that there's no resolution that's natural for everyone, and accepting a raw integer as a timespan is going to cause problems no matter what resolution is chosen--it's just too easy to get the number of zeros wrong. So I'd rather aim for establishing a structured form of time representation than to try and tweak the current setup. In the interim, Tango users will be happy to note that Thread.sleep() uses the same resolution as TimeSpan uses internally, so calling this routine from Tango using a TimeSpan should be pretty straightforward. SeanJohn C wrote:...I assume people won't need a higher resolution than milliseconds that often. Could Thread.sleep be changed into something like this? Thread.sleep(long millis, long hundredsOfNanos=0);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.
Nov 02 2008
Sean Kelly Wrote:torhu wrote:It is: import tango.time.Time, core.thread; Thread.sleep(TimeSpan.fromMillis(50).ticks);Sean Kelly wrote:While Windows uses milliseconds, *nix uses either micrososeconds (usleep) or a combination of seconds and nanoseconds (nanosleep), so while using milliseconds may be more natural for Windows users it's liable to confuse others. I suppose what I'm getting at is that there's no resolution that's natural for everyone, and accepting a raw integer as a timespan is going to cause problems no matter what resolution is chosen--it's just too easy to get the number of zeros wrong. So I'd rather aim for establishing a structured form of time representation than to try and tweak the current setup. In the interim, Tango users will be happy to note that Thread.sleep() uses the same resolution as TimeSpan uses internally, so calling this routine from Tango using a TimeSpan should be pretty straightforward. SeanJohn C wrote:...I assume people won't need a higher resolution than milliseconds that often. Could Thread.sleep be changed into something like this? Thread.sleep(long millis, long hundredsOfNanos=0);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.
Nov 02 2008
Sean Kelly Wrote:John C wrote:Not really - I mean that you should be able to set a Thread object's priority after creation but before start() is called. Currently this doesn't work - an exception is thrown. Perhaps store the requested priority in a field in the class, and SetPriority with that value on the handle after _beginthreadex?Sean Kelly Wrote:Do you mean as opposed to in its ctor? SeanAlso, any reason why priority is only available once the Thread's handle has been created - ie, after it's been started?Should I file a bug report?Don't bother. But thanks for the report.
Nov 01 2008
John C wrote:Sean Kelly Wrote:Thread.start() mostly exists to ensure that classes derived from Thread are properly constructed before the thread is started, so I don't encourage constructing threads and then letting them sit before starting them. For this reason, there is basically no support built into Thread for this "initialized but not started" state. There's an isRunning method but no state method that has an INIT state, for example. So while it would be possible to store thread priority info until the thread is started, this would really only support an approach that I don't really encourage anyway. I could probably be convinced to change it, but I'm not inclined to right now :) SeanJohn C wrote:Not really - I mean that you should be able to set a Thread object's priority after creation but before start() is called. Currently this doesn't work - an exception is thrown. Perhaps store the requested priority in a field in the class, and SetPriority with that value on the handle after _beginthreadex?Sean Kelly Wrote:Do you mean as opposed to in its ctor?Also, any reason why priority is only available once the Thread's handle has been created - ie, after it's been started?Should I file a bug report?Don't bother. But thanks for the report.
Nov 01 2008