C语言中的计时器\计数器用于费率计算?

发布于 2024-08-26 18:37:38 字数 277 浏览 6 评论 0原文

需要一个运行(移动、滚动)平均算法来计算传入的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑饮青盏花 2024-09-02 18:37:38

我之前答案的代码

#define REENTRANT
//The above is neccessary when using threads. This must be defined before any includes are made
//Often times, gcc -DREENTRANT is used instead of this, however, it produces the same effect

#include <pthread.h>

char running=1;

void* timer(void* dump){
    unsigned char i=0;
    while(running){
        for(i=0;i<300 && running;i++){
            sleep(1);//so we don't need to wait the 300 seconds when we want to quit
        }
        if(running)
           callback();//note that this is called from a different thread from main()
    }
    pthread_exit(NULL);
}

    int main(){
    pthread_t thread;
    pthread_create(&thread,NULL,timer,NULL);
    //do some stuff
    running=0;
    pthread_join(thread,NULL);//we told it to stop running, however, we might need to wait literally a second
    pthread_exit(NULL);
    return 0;
}

The Code to my Previous Answer

#define REENTRANT
//The above is neccessary when using threads. This must be defined before any includes are made
//Often times, gcc -DREENTRANT is used instead of this, however, it produces the same effect

#include <pthread.h>

char running=1;

void* timer(void* dump){
    unsigned char i=0;
    while(running){
        for(i=0;i<300 && running;i++){
            sleep(1);//so we don't need to wait the 300 seconds when we want to quit
        }
        if(running)
           callback();//note that this is called from a different thread from main()
    }
    pthread_exit(NULL);
}

    int main(){
    pthread_t thread;
    pthread_create(&thread,NULL,timer,NULL);
    //do some stuff
    running=0;
    pthread_join(thread,NULL);//we told it to stop running, however, we might need to wait literally a second
    pthread_exit(NULL);
    return 0;
}
柠檬心 2024-09-02 18:37:38

“愚蠢”的解决方案是使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文