Loops/timers in C

11,714

Solution 1

Simplest method available:

#include <pthread.h>

void *do_smth_periodically(void *data)
{
  int interval = *(int *)data;
  for (;;) {
    do_smth();
    usleep(interval);
  }
}

int main()
{
  pthread_t thread;
  int interval = 5000;

  pthread_create(&thread, NULL, do_smth_periodically, &interval)

  ...
}

Solution 2

On POSIX systems you can create (and catch) an alarm. Alarm is simple but set in seconds. If you need finer resolution than seconds then use setitimer.

struct itimerval tv;
tv.it_interval.tv_sec = 0;
tv.it_interval.tv_usec = 100000;  // when timer expires, reset to 100ms
tv.it_value.tv_sec = 0;
tv.it_value.tv_usec = 100000;   // 100 ms == 100000 us
setitimer(ITIMER_REAL, &tv, NULL);

And catch the timer on a regular interval by setting sigaction.

Solution 3

One doesn't "create a timer in C". There is nothing about timing or scheduling in the C standard, so how that is accomplished is left up to the Operating System.

This is probably a reasonable question for a C noob, as many languages do support things like this. Ada does, and I believe the next version of C++ will probably do so (Boost has support for it now). I'm pretty sure Java can do it too.

On linux, probably the best way would be to use pthreads. In particular, you need to call pthread_create() and pass it the address of your routine, which presumably contains a loop with a sleep() (or usleep()) call at the bottom.

Note that if you want to do something that approximates real-time scheduling, just doing a dumb usleep() isn't good enough because it won't account for the execution time of the loop itself. For those applications you will need to set up a periodic timer and wait on that.

Solution 4

SDL provides a cross platform timer in C.

http://www.libsdl.org/cgi/docwiki.cgi/SDL_AddTimer

Share:
11,714

Related videos on Youtube

some_id
Author by

some_id

Updated on June 17, 2020

Comments

  • some_id
    some_id almost 4 years

    How does one create a timer in C?

    I want a piece of code to continuously fetch data from a gps parsers output.

    Are there good libraries for this or should it be self written?

    • some_id
      some_id
      Dont worry about the parsing. I would just like to know how to create a timer function in C which loops and executes its statements.
  • some_id
    some_id about 13 years
    So how would I execute a function in a loop in its own thread?
  • T.E.D.
    T.E.D. about 13 years
    @Helium3 - Either use OS calls (which depend on what OS you are running, which you have yet to tell us), or use a better language.
  • some_id
    some_id about 13 years
    It will be run on Ubuntu on a Pandaboard.
  • stijn
    stijn about 13 years
    [pedantic] a 'better' language is defined by having timing support? funny.
  • T.E.D.
    T.E.D. about 13 years
    @stijn - For this particular purpose, yes a language that has actual support for threading (even in a portable library like boost) would be better. C has its stregths too, but this isn't one of them.
  • T.E.D.
    T.E.D. about 13 years
    It appears to be a framework designed around multimedia. He'd have to use its "mainloop" and set up a timer callback. Could be useful, if that kind of structure is acceptable.
  • Null Set
    Null Set about 13 years
    @T.E.D. It is designed for multimedia, but I've found its timers and its event queue to be handy on their own.
  • T.E.D.
    T.E.D. about 13 years
    Not downvoting, but he did also mention that he wanted it run in a separate thread, so the code to create a thread and run do_smth_periodically() in it would also be helpful. Also, this method will cause the entire "wall" time of the loop to vary depending on how much time it takes to do do_smth() each iteration. Most likely that's fine for his application though.
  • natenho
    natenho over 7 years
    Race condition warning: Be careful to keep interval variable in memory when calling pthread_create from another function instead of main(), because interval may become out of scope before the thread assignment of *data argument in the first line.

Related