使用?c++?在?windows?上定時執(zhí)行一個函數(shù)的示例代碼
#include <iostream>
#include <Windows.h>
#include <thread>
#pragma comment( lib, "Winmm" )
static int counter = 0;
static int64_t ticks_per_second;
void __stdcall on_timer(HWND h, UINT ui, UINT_PTR up, DWORD dw)
{
std::cout << "time out, counter=" << counter << std::endl;
counter = 0;
}
void get_message_trd_func()
{
SetTimer(NULL, 0, 1000, on_timer);
MSG msg;
while (GetMessageA(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessageA(&msg);
}
}
int main()
{
std::cout << "go!" << std::endl;
timeBeginPeriod(1);
QueryPerformanceFrequency((LARGE_INTEGER*)&ticks_per_second);
const double expected = 1.0 / 60.0;
const int64_t expected_ticks = (int64_t)(expected * ticks_per_second);
std::thread thr(get_message_trd_func);
for (;;)
{
int64_t before_ticks = 0;
QueryPerformanceCounter((LARGE_INTEGER*)&before_ticks);
// do something...
for (int i = 0; i < 10000; i++)
{
float a = i * i + i + sin(i) + sqrt(i);
}
int64_t after_ticks = 0;
QueryPerformanceCounter((LARGE_INTEGER*)&after_ticks);
counter++;
int64_t ticks_need_sleep = expected_ticks - (after_ticks - before_ticks);
double ms_need_sleep = (double)ticks_need_sleep / ticks_per_second * 1000.0;
if (ms_need_sleep >= 1.0)
Sleep((DWORD)ms_need_sleep);
else
continue;
}
}這里主要用到的幾個 win32api 為
MMRESULT timeBeginPeriod使用該 api 需要鏈接
Winmm, 所以我們在文件頂部加入#pragma comment( lib, "Winmm" ).
它的作用是請求提高一些計時器的精度比如這里的Sleep, 默認 windows 似乎只會給我們提供 10ms 左右很粗糙的精度, 所以這里我們直接請求盡可能的高的精度, 即1ms.QueryPerformanceFrequency該 api 用于獲取"性能計時器"的精度, 單位是 ticks每秒. 在我的機子上它的值是
10000000, 可以看到精度還是很令人滿意的. 在這里我們將其與expected(也就是期望每次調(diào)用的間隔,單位s)進行相乘, 得到一個以ticks為單位的間隔.QueryPerformanceCounter該 api 會檢測"性能計時器"的值, 單位為 ticks, 在 msdn 中其精度的描述為 <1us.
那么結(jié)合上述幾個 api 以及幾個簡單的數(shù)學運算, 這樣就可以相對穩(wěn)定的定時調(diào)用函數(shù)了(在這里是 1s 60 次):
time out, counter=59 time out, counter=59 time out, counter=60 time out, counter=59 time out, counter=58 time out, counter=60
當你注釋掉timeBeginPeriod的調(diào)用后你會發(fā)現(xiàn)結(jié)果不是很樂觀(即使我們期望 1s 調(diào)用 60 次):
time out, counter=33 time out, counter=31 time out, counter=32 time out, counter=31
最后, 這個可能常見于游戲的幀率控制, 實際上我就是從這里知道的這些東西(x
到此這篇關于使用 c++ 在 windows 上穩(wěn)定定時執(zhí)行一個函數(shù)的文章就介紹到這了,更多相關c++定時執(zhí)行函數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C++實現(xiàn)LeetCode(106.由中序和后序遍歷建立二叉樹)
這篇文章主要介紹了C++實現(xiàn)LeetCode(106.由中序和后序遍歷建立二叉樹),本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-07-07
在vs2010中,輸出當前文件路徑與源文件當前行號的解決方法
本篇文章是對在vs2010中,輸出當前文件路徑與源文件當前行號的解決方法進行了詳細的分析介紹,需要的朋友參考下2013-05-05

