Loops/timers in C

2019-01-22 23:16发布

问题:

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?

回答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)

  ...
}


回答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.



回答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.



回答4:

SDL provides a cross platform timer in C.

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



回答5:

If your using Windows, you can use SetTimer,else you can build a timer out of timeGetTime and _beginthreadex along with a queue of timers with callbacks



回答6:

The question about a timer is quite unspecific, though there are two functions that come to my mind that will help you:

  • sleep() This function will cause execution to stop for a specified number of seconds. You can also use usleep and nanosleep if you want to specify the sleeptime more exactly
  • gettimeofday() Using this function you are able to stop between to timesteps.

See manpages for further explanation :)



回答7:

If the gps data is coming from some hardware device, like over a serial port, then one thing that you may consider is changing the architecture around so that the parser kicks off the code that you are trying to run when more data is available.

It could do this through a callback function or it could send an event - the actual implementation would depend on what you have available.