flc 是一个频率控制器,用来解决一些需要控制函数单位时间调用次数的问题。
// frequency limit controller FLC
#include <chrono>
#include <ratio>
#include <thread>
// FPS or Hz per second
/*!
FLC<5> flc;//5Hz
flc.run();
*/
template<std::intmax_t FPS>
class FLC
{
using clock = std::chrono::steady_clock;
using frames = std::chrono::duration<std::int64_t, std::ratio<1, FPS>>; // std::ratio<1, FPS> seconds
public:
void run() {
while (true)
{
next_frame += frame;
std::cout << std::time(0) << '\n'; // 5 for each second
// wait for end of frame
std::this_thread::sleep_until(next_frame);
}
}
private:
clock::time_point next_frame = clock::now();
frames frame{ 1 };
};
本文介绍了一种名为FLC的频率控制器模板,它使用C++编程实现,用于解决需要限制函数每秒调用次数的问题,适用于控制周期性任务执行。通过实例说明如何创建一个以5Hz频率运行的控制器,并确保任务在指定频率下稳定执行。
1007

被折叠的 条评论
为什么被折叠?



