如何安装重复发生的计时器功能
How to install a reoccurring timer function?
是否有一种简单的方法可以使用C /stdlib安装定期出现的计时器函数?我想摆脱循环:
using namespace std::chrono; // literal suffixes
auto tNext = steady_clock::now();
while (<condition>) {
std::this_thread::sleep_until(tNext);
tNext = tNext + 100ms;
...
该功能将以自己的线程运行。
我猜你想要什么
int i = 10;
auto pred = [i]() mutable {return i--;};
auto print = []{cout << "." << endl;};
timer t{500ms};
t.push({print, pred}); //asynchronously prints '.' 10 times within 5s
//do anything else
假设性能并不关键,并且计时器不经常更新,以下内容应提供足够的功能。
#include<functional>
#include<vector>
#include<thread>
#include<utility>
#include<chrono>
#include<mutex>
#include<atomic>
class timer final
{
public:
using microseconds = std::chrono::microseconds;
using predicate = std::function<bool ()>;
using callback = std::function<void ()>;
using job = std::pair<callback, predicate>;
explicit timer(microseconds t) : done{false}, period{t}
{
std::lock_guard<std::mutex> lck(mtx);
worker = std::thread([this]{
auto t = std::chrono::steady_clock::now();
while(!done.load())
{
std::this_thread::sleep_until(t);
std::lock_guard<std::mutex> lck(mtx);
t += period;
for(auto it = jobs.begin(); it != jobs.end();)
{
if(it->second())
it++->first();
else
it = jobs.erase(it);
}
}
});
}
~timer()
{
done.store(true);
worker.join();
}
void set_period(microseconds t)
{
std::lock_guard<std::mutex> lck(mtx);
period = t;
}
void push(const callback& c)
{
std::lock_guard<std::mutex> lck(mtx);
jobs.emplace_back(c, []{return true;});
}
void push(const job& j)
{
std::lock_guard<std::mutex> lck(mtx);
jobs.push_back(j);
}
private:
std::mutex mtx;
std::atomic_bool done;
std::thread worker;
std::vector<job> jobs;
microseconds period;
};
timer
先前定期按callback
s的调用,当predicate
评估false
时,将callback
从timer
删除。timer
对象具有自己的寿命,只要活着,它的工作线程就会活。
您希望在单个timer
中拥有多个job
s的原因是这样可以将它们调用,仅使用一个线程并彼此同步。
,除非您打算每秒更新计时器> 10,000次,否则请不要担心mutex
,有一个lt; 1ms的时期或耗时的callback
s。
相关文章:
- Linux的Cpp上的计时器
- 提升 ASIO 无法识别计时器对象
- 提升 asio 并发计时器取消问题与链
- 使用单体计时器的pthread_cond_timedwait有时会比预期晚超时
- 窗口中的微秒计时器
- 计时器是否从另一个线程启动?
- 如何在 c++ 中创建计时器
- C++回调计时器实现
- 在计时器或主线程外部的命令上销毁/替换线程
- 如何制作每秒从 30 乘 1 倒计时的计时器?
- 保留计时器集合(对象与指针)的最佳方法
- 在网络套接字计时器滴答后增加asio短读错误
- 是否可以仅使用标准 c++/c++11 实现不带"sleep"的计时器?
- 在没有NtSetTimerResolution的Windows上提高计时器分辨率(高分辨率)
- 功能计时器阻止主功能继续
- 如何为带有计时功能的程序创建计时器?
- 如何安装重复发生的计时器功能
- Glut计时器/空闲功能未更新
- Glut计时器功能
- Windows计时器功能的性能