C语言中的计时器\计数器用于费率计算?
需要一个运行(移动、滚动)平均算法来计算传入的 5 分钟平均位。我所需要处理的只是传入位的累积值。
例如:我从 0 位开始, 5 分钟后,我有 10 位,所以我的平均值是 10 位。 5 分钟后,我有 15 位,所以现在我的平均值是 7.5 位。又过 5 分钟后,我有 30 位,所以我现在的平均值是 10.8 位。
我的问题是,如何在 C++ 中实现计时器\计数器,以便它以精确的 5 分钟间隔轮询位值?显然我不能使用延迟300秒。但是我可以在后台创建一个计时器,每 5 分钟只触发一个事件(轮询位值)吗?
Need a running (moving, rolling) average algorithm to calculate the 5-minute average bits that are passed in. All I have to work with is an accumulative value for the bits that are passed in.
For example: I start with 0 bits, 5 minutes later, I have 10 bits, so my average is 10 bits. 5 minutes later, I have 15 bits, so now my average is 7.5 bits. Another 5 minutes later, I have 30 bits, so my average now is 10.8 bits.
My question is, how can I implement a timer\counter in C++ so it would poll the bits value in exact 5 minutes intervals? Obviously I can't use delay 300 seconds. But can I make a timer in the background which would only fire an event (poll the bit value) every 5 minutes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我之前答案的代码
The Code to my Previous Answer
“愚蠢”的解决方案是使用 POSIX 线程。您可以创建一个线程,然后将其放入带有 sleep() 的无限循环中。
The "dumb" solution is to use POSIX threads. You can make a thread and then put it in an infinite loop with sleep() in it.