c++使用线程作为定时器
我的项目有一个不同的线程来制作一些东西。
没关系,但我希望每 20 毫秒调用一些函数(例如),但考虑到该函数花费的时间...我尝试更好地解释...我调用该函数,该函数花了 6 毫秒退出,所以我希望线程休眠 14 毫秒(20 - 6),下次调用时,该函数花费 12 毫秒,因此休眠时间仅为 8 毫秒.... 我该怎么做? 这是我的代码:
// thread constructor
DWORD dwThreadId, dwThrdParam = 1;
HANDLE thread = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)workerFunc, &dwThrdParam, 0, &dwThreadId );
// thread function
DWORD WINAPI workerFunc( LPDWORD lpdwParam )
{
while( true )
{
myFunction( );
Sleep( ??? );
}
return 0;
}
谢谢!
I have my project with a different thread that make some stuff.
It's all ok, but I wish have some function called each 20ms (for example), but considering the time spent to the function... I try to explain better... I call the function, and the function spent 6ms to exit, so I wish that the thread sleeps 14ms (20 - 6), the next time that is called, the function spent 12ms, so the sleep will be only 8ms....
how can I do this?
here is my code:
// thread constructor
DWORD dwThreadId, dwThrdParam = 1;
HANDLE thread = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)workerFunc, &dwThrdParam, 0, &dwThreadId );
// thread function
DWORD WINAPI workerFunc( LPDWORD lpdwParam )
{
while( true )
{
myFunction( );
Sleep( ??? );
}
return 0;
}
thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这个怎么样:
What about this:
一种简单的方法是让线程休眠一段可变的时间:
您需要一个适合
get_time()
的稳定时钟函数和一个 sleep 函数 - 尝试< chrono>
或
标头。在这个例子中,我假设你有一个计时器回调(就像在 boost.ASIO 中),但你也可以创建一个无限循环或类似的东西。如果函数体从未超过 20 毫秒,则此函数将起作用,但如果您的函数花费的时间更长,它会快速赶上。如果你想同步到全局 20 毫秒节奏,你必须添加另一个全局计步器或类似的东西。
A simple approach would be to send the thread to sleep for a variable amount of time:
You'll need a suitable steady clock function for
get_time()
and a sleep function - try the<chrono>
or<tr1/chrono>
header. In this example I've assumed that you have a timer callback (like in boost.ASIO), but you could also make an infinite loop or something like that.This function will work if the function body never exceeds 20ms, though it'll do a quick catch-up if your function ever takes longer. If you want to synchronize to a global 20ms rhythm, you'd have to add another global step counter or something like that.
在调用
之前使用 GetTickCount myFunc
以及调用myFunc
之后。差值给出了在myFunc
中花费的时间。请注意,该时间包括进程由于操作系统调度而花费的时间。Use GetTickCount before calling
myFunc
and after callingmyFunc
. The difference gives you the time spent inmyFunc
. Note that this time includes the time spent by the process due to OS scheduling.如果需要精度,请在程序启动时使用
NtSetTimerResolution
来设置计时器分辨率。是的,它是未记录的功能,但效果很好。您还可以使用 NtQueryTimerResolution 来了解计时器分辨率(在设置之前和设置新分辨率之后确保)。您需要使用
NTDLL.DLL
中的GetProcAddress
动态获取这些函数的地址,因为它没有在标头或任何 LIB 文件中声明。以这种方式设置计时器分辨率会影响睡眠、Windows 计时器、返回当前时间的函数等。
If you need precision, use
NtSetTimerResolution
when program starts up to set timer resolution. Yes, it is undocumented function, but works well. You may also useNtQueryTimerResolution
to know timer-resolution (before setting and after setting new resolution to be sure).You need to dynamically get the address of these functions using
GetProcAddress
fromNTDLL.DLL
, as it is not declared in header or any LIB file.Setting timer resolution this way would affect
Sleep
, Windows timers, functions that return current time etc.