Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 715053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:08:38+00:00 2026-05-14T05:08:38+00:00

Need a running (moving, rolling) average algorithm to calculate the 5-minute average bits that

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T05:08:38+00:00Added an answer on May 14, 2026 at 5:08 am

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PHP script with infinite loop. I need this script running forever.
If I don't do any map/reduce jobs, still JobTracker/TaskTrackers need to be running for
I need to use maps on running in a physical Android device, how do
We need to change an already running job. We should be able to push
I need to use an embedded system running Python 1.5.2+ (!!!) with very few
I need to get up and running quickly with a MT TCP server implemented
I need to know which stored procedures are running. It's not an option to
I need to launch a number of long-running processes with subprocess.Popen , and would
I need to find out which account is running one of my websites application
I have a java web app running under Spring 2.5.6, Hibernate 3.4 (with Hibernate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.