Yos*_*sry 2 c++ multithreading sleep asynchronous delay
我正在使用C++编写一个简单的游戏模拟程序,有一个名为update()的函数可以更新游戏的当前状态,它必须每1秒精确调用一次.如果我使用这样的循环:
while(//some condition) {
update();
Sleep(1000);
}
Run Code Online (Sandbox Code Playgroud)
然后,每1秒钟不会调用该函数,而是每次调用(1 +执行update()).我阅读了各种解决方案,如异步函数,多线程,或使用std :: chrono计算函数的执行时间,并将其从1000ms参数减去sleep.其中一些对我的简单案例来说太复杂了,如果我不理解它们,其他一些似乎不安全.
有谁能告诉我什么是我的要求的合适解决方案?提前致谢.
相反,睡一段时间,你需要睡觉,直到一个时间点.例如,如果您的第一次更新时间恰好是2:00:00.000,则您的未来更新应尽可能接近2:00:01.000,2:00:02.000等.
为实现此目的,您可以将一个线程专用于更新,并在更新后进入休眠状态,直到下次执行预定更新. chrono::system_clock::time_point并且this_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);
}
Run Code Online (Sandbox Code Playgroud)
仅用于演示目的(逻辑不需要),我使用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
Run Code Online (Sandbox Code Playgroud)