Scheduling A Function To Be Called Every N Milliseconds?

I'd like to call a particular C++ function regularly every N milliseconds while the program is running (perhaps up to a limit of K iterations). For instance the function scheduledCall() would be called every 1000 milliseconds, until it had been called 500 times. How would I do this?

Comments

  • If you don't mind a rough timer, you might try something like

    [code]
    int scheduledCall(void) {

    static int count = 0;

    /* function task(s) */

    return (count++);
    }

    int main(void) {

    time_t t1, t2;
    long nsecs = 1; // ~ 1000 ms

    while(scheduledCall() < 500) {
    t1 = time(NULL);
    do { t2 = time(NULL); } while((t1 + nsecs) > t2);
    }

    return 0;
    }[/code]

    You could also keep a local counter instead of scheduledCall returning a value.

    HTH

  • This post has been deleted.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories