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 119811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:42:43+00:00 2026-05-11T03:42:43+00:00

I need a timer to execute callbacks with relatively low resolution. What’s the best

  • 0

I need a timer to execute callbacks with relatively low resolution. What’s the best way to implement such C++ timer class in Linux? Are there any libraries I could use?

  • 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. 2026-05-11T03:42:44+00:00Added an answer on May 11, 2026 at 3:42 am

    If you’re writing within a framework (Glib, Qt, Wx, …), you’ll already have an event loop with timed callback functionalities. I’ll assume that’s not the case.

    If you’re writing your own event loop, you can use the gettimeofday/select pair (struct timeval, microsecond precision) or the clock_gettime/nanosleep pair (struct timespec, nanosecond precision) for your own event dispatcher. Even though latter interface is higher resolution, scheduling is never that accurate anyways, so take whatever fits best.

    #include <algorithm> #include <functional> #include <vector>  #include <errno.h> #include <sys/time.h> #include <unistd.h>  using namespace std;  class scheduler { public:     scheduler();     int events();     void addEvent(const struct timeval, int (*)(void *), void *);     int dispatchUntil(const struct timeval &);     bool waitUntil(const struct timeval * = NULL);     int loopUntil(const struct timeval * = NULL);  private:     static bool tv_le(const struct timeval &, const struct timeval &);     struct event {         struct timeval when;         int (*callback)(void *);         void *data;     };     static struct _cmp       : public binary_function<bool, const struct event &, const struct event &>     {         bool operator()(const struct event &a, const struct event &b) {             return !tv_le(a.when, b.when);         }     } cmp;     vector<struct event> heap; };  bool scheduler::tv_le(const struct timeval &a, const struct timeval &b) {     return a.tv_sec < b.tv_sec ||         a.tv_sec == b.tv_sec && a.tv_usec <= b.tv_usec; }  scheduler::scheduler() : heap() {}  int scheduler::events() {     return heap.size(); }  void scheduler::addEvent(const struct timeval when, int (*callback)(void *), void *data) {     struct event ev = {when, callback, data};     heap.push_back(ev);     push_heap(heap.begin(), heap.end(), cmp); }  int scheduler::dispatchUntil(const struct timeval &tv) {     int count = 0;     while (heap.size() > 0 && tv_le(heap.front().when, tv)) {         struct event ev = heap.front();         pop_heap(heap.begin(), heap.end(), cmp);         heap.pop_back();         ev.callback(ev.data);         count++;     }     return count; }  bool scheduler::waitUntil(const struct timeval *tv) {     if (heap.size() > 0 && (!tv || tv_le(heap.front().when, *tv)))         tv = &heap.front().when;     if (!tv)         return false;     struct timeval tv2;     do {         gettimeofday(&tv2, NULL);         if (tv_le(*tv, tv2))             break;         tv2.tv_sec -= tv->tv_sec;         if ((tv2.tv_usec -= tv->tv_usec) < 0) {             tv2.tv_sec--;             tv2.tv_usec += 1000000;         }     } while (select(0, NULL, NULL, NULL, &tv2) < 0 && errno == EINTR);     return heap.size() > 0 && tv_le(*tv, heap.front().when); }  int scheduler::loopUntil(const struct timeval *tv) {     int counter = 0;     while (waitUntil(tv))         counter += dispatchUntil(heap.front().when);     return counter; } 

    Warning: I love C. I never write C++. I’m just pretending to know the language.

    Disclaimer: written just now and totally untested. The basic idea is to keep events in a priority queue, wait until the first one, run it, and repeat.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 109k
  • Answers 109k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I recommend you to split this into two queries. First,… May 11, 2026 at 9:22 pm
  • Editorial Team
    Editorial Team added an answer Using anonymous delegate: Guid feedID = ...; RssFeedDocument document =… May 11, 2026 at 9:22 pm
  • Editorial Team
    Editorial Team added an answer This is possible to do in MySQL, but it takes… May 11, 2026 at 9:22 pm

Related Questions

I need a timer to execute callbacks with relatively low resolution. What's the best
I can't get to the bottom of this error, because when the debugger is
What is the proper technique to have ThreadA signal ThreadB of some event, without
Ok this is a little complex. I am creating a plugin, and want to
I have a servlet S which handles callbacks from a 3rd party site. The

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.