c++使用线程作为定时器

发布于 2024-11-24 10:29:00 字数 552 浏览 10 评论 0原文

我的项目有一个不同的线程来制作一些东西。

没关系,但我希望每 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

柏拉图鍀咏恒 2024-12-01 10:29:00

这个怎么样:

clock_t start, end;
while( true )
{
    start = clock();
    myFunction( );
    end = clock();
    Sleep(20-(end-start)*CLOCKS_PER_SEC/1000.0);
}

What about this:

clock_t start, end;
while( true )
{
    start = clock();
    myFunction( );
    end = clock();
    Sleep(20-(end-start)*CLOCKS_PER_SEC/1000.0);
}
清风夜微凉 2024-12-01 10:29:00

一种简单的方法是让线程休眠一段可变的时间:

void periodic_function()
{
  /* function body */

  timestamp = get_time(); // pseudo-function
  remaining_sleep_time = std::min(0, 20 - (timestamp - last_timestamp));
  last_timestamp = timestamp;
  milli_sleep_callback(remaining_sleep_time, periodic_function);
}

您需要一个适合 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:

void periodic_function()
{
  /* function body */

  timestamp = get_time(); // pseudo-function
  remaining_sleep_time = std::min(0, 20 - (timestamp - last_timestamp));
  last_timestamp = timestamp;
  milli_sleep_callback(remaining_sleep_time, periodic_function);
}

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.

冰雪梦之恋 2024-12-01 10:29:00

在调用 之前使用 GetTickCount myFunc 以及调用 myFunc 之后。差值给出了在 myFunc 中花费的时间。请注意,该时间包括进程由于操作系统调度而花费的时间。

Use GetTickCount before calling myFunc and after calling myFunc. The difference gives you the time spent in myFunc. Note that this time includes the time spent by the process due to OS scheduling.

雨巷深深 2024-12-01 10:29:00

如果需要精度,请在程序启动时使用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 use NtQueryTimerResolution 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 from NTDLL.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文