每 1 秒(准确地)调用一个函数

Calling a function every 1 second (precisely)

本文关键字:调用 函数 一个      更新时间:2023-10-16

我正在C++中开发一个简单的游戏模拟程序,有一个名为update((的函数可以更新游戏的当前状态,它必须精确地每1秒调用一次。如果我使用这样的循环:

while(//some condition) {
update();
Sleep(1000);
}

然后不会每 1 秒调用一次函数,而是每 (1 + 更新 (( 的执行时间(。 我阅读了各种解决方案,例如异步函数、多线程或使用 std::chrono 计算函数的执行时间并从 1000ms 参数中减去它进入睡眠状态。其中一些对于我的简单案例来说太复杂了,而如果我不太了解它们,其他的似乎不安全。

谁能告诉我什么适合我的需求? 提前谢谢。

与其睡一段时间,不如睡一个时间点。 例如,如果第一次更新恰好在 2:00:00.000,则将来的更新应尽可能接近 2:00:01.000、2:00:02.000 等。

为此,您可以将一个线程专用于更新,并在更新后进入睡眠状态,直到下次执行计划更新。chrono::system_clock::time_pointthis_thread::sleep_until是您执行此操作的工具。

例如:

#include <atomic>
#include <chrono>
#include <iostream>
#include <thread>
class UpdateManager
{
public:
explicit UpdateManager() = default;
private:
static std::atomic<int> now_;
static std::atomic<bool> stop_;
struct update_thread
: private std::thread
{
~update_thread();
update_thread(update_thread&&) = default;
using std::thread::thread;
};
public:
static update_thread start();
};
void update();
// source
std::atomic<int>  UpdateManager::now_{0};
std::atomic<bool> UpdateManager::stop_{false};
UpdateManager::update_thread::~update_thread()
{
if (joinable())
{
stop_ = true;
join();
}
}
UpdateManager::update_thread
UpdateManager::start()
{
return update_thread{[]
{
using namespace std;
using namespace std::chrono;
auto next = system_clock::now() + 1s;
while (!stop_)
{
update();
this_thread::sleep_until(next);
next += 1s;
}
}};
}
#include "date/date.h"
void
update()
{
using namespace date;
using namespace std;
using namespace std::chrono;
cerr << system_clock::now() << 'n';
}
// demo
int
main()
{
auto t = UpdateManager::start();
using namespace std;
this_thread::sleep_for(10s);
}

仅出于演示目的(逻辑不是必需的(,我使用 Howard Hinnant 的免费开源日期/时间库将当前时间 (UTC( 打印到微秒精度,以说明这种技术的稳定性。 此程序的示例输出为:

2018-05-02 15:14:25.634809
2018-05-02 15:14:26.637934
2018-05-02 15:14:27.636629
2018-05-02 15:14:28.637947
2018-05-02 15:14:29.638413
2018-05-02 15:14:30.639437
2018-05-02 15:14:31.637217
2018-05-02 15:14:32.637895
2018-05-02 15:14:33.637749
2018-05-02 15:14:34.639084

您可以避免将 chrono 用于此更简单的选项。但是,如果你想要μs-ns的精度,我强烈建议使用chrono。

#include <ctime>
while(//some condition) {
std::clock_t start = std::clock();
update();
std::clock_t end   = std::clock();
Sleep(1000 - (end - start)/CLOCKS_PER_SEC * 1000);
}

确保某些事情在正确的时间发生的最佳方法是使用 while 循环中的this_thread::sleep_until(),正如其他一些人所建议的那样。

我喜欢确保我的循环从秒的底部开始,因此使用时间time_since_epoch计算next_full_second时间点

interval变量可以设置为秒或毫秒。使用HowardHinnant的开源日期库来打印出漂亮的时间值。

#include <chrono>
#include <iostream>
#include <thread>
#include "date/date.h"
using namespace date;
using namespace std;
using namespace std::chrono;
int main()
{
system_clock::time_point now = system_clock::now();
auto s = duration_cast<seconds>(now.time_since_epoch());
system_clock::time_point next_full_second = system_clock::time_point(++s);
auto interval = seconds(1); // or milliseconds(500) or whatever
auto wait_until = next_full_second;
while (1)
{
this_thread::sleep_until(wait_until);
cout << system_clock::now() << endl;
// do something
wait_until += interval;
}
return 0;
}

请参阅此伪代码。如果每个进程(可能需要超过 1 秒(都是在单独的线程中异步完成的,您可以根据需要在下一个循环之前正好休眠 1 秒。

void MainGameLoop(bool& running)
{
while (running)
{
AnimateSpritesAsync();
AnimateBulletsAsync();
CheckCollisionAsync();
CheckWinConditionsAsync();
WaitPreciselyOneSecond();
}
}

例如,您的update函数将如下所示:

void update(); //your update 
void UpdateAsync()  // the async update
{
std::thread([]() { update(); }).detach();
}

现在,您可以:

void MainGameLoop(bool& running)
{
while (running)
{
UpdateAsync();    
Sleep(1000); // i won't recommend Sleep tho
}
}