问题描述
我正在尝试创建一个可以在短时间内(少于1毫秒)激活的计时器.
我从www.sysinternals.com找到了一个名为"ClockRes v2.0"的应用程序.
该应用程序报告了
最大计时器间隔15.6毫秒
最小计时器间隔0.5毫秒
当前计时器间隔15.6 ms.
这意味着可以更改系统时钟分辨率.但是我失败了.
接下来,我尝试使用此代码创建计时器.(从注册表读取频率,禁用CPU Boost Speed和多线程技术)
I''m trying to create a timer that activate in a small time(less than 1ms).
I found a application from www.sysinternals.com called "ClockRes v2.0"
That application reported
Maximum timer Interval 15.6 ms
Minimum timer Interval 0.5 ms
Current timer Interval 15.6 ms.
That means, It''s possible to change system clock resolution.but I''m fail.
Next, I try this code to create my timer.(read freq from registry,disabled CPU Boost Speed and multithread technology)
void MicroTmr(double ms)
{
unsigned __int64 start_in = 0;
unsigned __int64 stop_in= 0;
if (!SetPriorityClass(GetCurrentProcess(),REALTIME_PRIORITY_CLASS))
cout << "Set Prioity Class Fail. " << endl;
start_in = __rdtsc();
while(true)
{
stop_in = __rdtsc();
if (stop_in-start_in > freq/1000*ms)
break;
}
start_in = 0;
stop_in = 0;
}
但是,如果我将此功能设置为与程序位于同一内核中,则此功能将极大地干扰程序.(我也将程序的优先级设置为REALTIME_PRIORITY_CLASS)
我的问题是:
1.如何更改系统时钟分辨率?
2.如何保护我的程序不受干扰?
3.有什么方法可以在短间隔(小于1毫秒)内使用计时器
非常感谢您的回答.
but, If I set this function and my program in a same core this function will extremely disturb my program.(I set my program''s priority at REALTIME_PRIORITY_CLASS too)
My question is :
1. How can I change system clock resolution?
2. How can I protect my program from the disturbance?
3. Are there any ways to use timer in a small interval(Less than 1ms)
Very thank you for your answer.
推荐答案
typedef NTSTATUS (CALLBACK* LPFN_NtQueryTimerResolution)(PULONG,PULONG,PULONG);
typedef NTSTATUS (CALLBACK* LPFN_NtSetTimerResolution)(ULONG,BOOLEAN,PULONG);
HMODULE hNtDll = ::GetModuleHandle(_T("Ntdll"));
if (hNtDll)
{
ULONG nMinRes, nMaxRes, nCurRes;
LPFN_NtQueryTimerResolution pQueryResolution =
(LPFN_NtQueryTimerResolution)::GetProcAddress(hNtDll, "NtQueryTimerResolution");
if (pQueryResolution &&
pQueryResolution(&nMinRes, &nMaxRes, &nCurRes) == STATUS_SUCCESS)
{
TRACE(_T("NT timer resolutions (min/max/cur): %u.%u / %u.%u / %u.%u ms"),
nMinRes / 10000, (nMinRes % 10000) / 10,
nMaxRes / 10000, (nMaxRes % 10000) / 10,
nCurRes / 10000, (nCurRes % 10000) / 10);
}
LPFN_NtSetTimerResolution pSetResolution =
(LPFN_NtSetTimerResolution)::GetProcAddress(hNtDll, "NtSetTimerResolution");
if (pSetResolution && nSetRes)
{
NTSTATUS nStatus = pSetResolution(nSetRes, TRUE, &nCurRes);
}
}
这篇关于系统时钟分辨率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!