Feature #3587
closedwindows: unable to sleep < 0.015s
Description
=begin
sleep 0.0001; Hitimes::Interval.measure { 2.times { sleep 0.000001}}
=> 0.03124279761813303
(it's always 0.03s)
This surprised me a bit, since it differed from Linux behavior.
Thanks!
=end
Updated by kosaki (Motohiro KOSAKI) over 13 years ago
=begin
Hi
2010/7/19 Roger Pack redmine@ruby-lang.org:
Bug #3587: windows: unable to sleep < 0.015s
http://redmine.ruby-lang.org/issues/show/3587Author: Roger Pack
Status: Open, Priority: Normal
ruby -v: ruby 1.9.3dev (2010-07-19 trunk 28684) [i386-mingw32]sleep 0.0001; Hitimes::Interval.measure { 2.times { sleep 0.000001}}
=> 0.03124279761813303(it's always 0.03s)
This surprised me a bit, since it differed from Linux behavior.
Thanks!
Typically, windows has 1/64sec (15.625ms) tick resolution. but linux has
1/1000sec tick resolution. So, I don't surprised this so much.
Do you have any improvement idea?
=end
Updated by raggi (James Tucker) over 13 years ago
=begin
On 19 Jul 2010, at 16:19, KOSAKI Motohiro wrote:
Hi
2010/7/19 Roger Pack redmine@ruby-lang.org:
Bug #3587: windows: unable to sleep < 0.015s
http://redmine.ruby-lang.org/issues/show/3587Author: Roger Pack
Status: Open, Priority: Normal
ruby -v: ruby 1.9.3dev (2010-07-19 trunk 28684) [i386-mingw32]sleep 0.0001; Hitimes::Interval.measure { 2.times { sleep 0.000001}}
=> 0.03124279761813303(it's always 0.03s)
This surprised me a bit, since it differed from Linux behavior.
Thanks!Typically, windows has 1/64sec (15.625ms) tick resolution. but linux has
1/1000sec tick resolution. So, I don't surprised this so much.Do you have any improvement idea?
I am told there is a better way to do this (iirc, some media api), but, this is aged code I have for it:
#include "stdafx.h"
#include <Windows.h>
typedef LONG NTSTATUS;
typedef NTSTATUS (__stdcall *NSTR)(ULONG, BOOLEAN, PULONG);
#define STATUS_SUCCESS ((NTSTATUS)0x00000000L)
BOOL NtSetTimerResolution
(
IN ULONG RequestedResolution,
IN BOOLEAN Set,
OUT PULONG ActualResolution
);
int _tmain(int argc, _TCHAR* argv[])
{
HMODULE hNtDll = ::LoadLibrary(_T("ntdll.dll"));
BOOL fRetVal = FALSE;
ULONG RequestedResolution = 10000, ActualResolution = 0;
BOOL Set = true;
if (hNtDll)
{
NSTR pNSTR = (NSTR)::GetProcAddress(hNtDll, "NtSetTimerResolution");
if (pNSTR)
{
NTSTATUS nts = (*pNSTR)(RequestedResolution, Set, &ActualResolution);
if (STATUS_SUCCESS == nts)
while (true)
Sleep(100000);
else
return 1;
}
}
}
If the API is restricted by timer resolution, this code running as an application on the system should open that up to 1ms, other methods for doing this include running flash or opening media player.
I believe there are ways to do better, but I had another purpose for this code running as a service.
Roger - if this helps, I can provide a patch to hitimes, let me know.
=end
Updated by rogerdpack (Roger Pack) over 13 years ago
- Status changed from Open to Closed
=begin
This isn't a problem on windows 7, so...unless there's an easier way to do this in windows (or interest from any one else but myself), closing for now.
=end