Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run something every t seconds in C?

Tags:

c

timer

In C, I want a block of statements to be executed repeatedly every t seconds. How do I do this?

like image 888
trinity Avatar asked Jan 12 '10 15:01

trinity


2 Answers

This cannot be done in standard C, you need to use some platform-specific API.

One popular choice is POSIX' alarm() function.

This is a "pure" aynchronuous solution. It's of course possible to measure and handle time in other ways, but they're still platform-dependent. You could use sleep() (again, POSIX) to just block. If you combine that with your desired statements in a loop, that should work too.

like image 121
unwind Avatar answered Nov 16 '22 20:11

unwind


You will need to create a thread or process that runs a loop containing a wait request.

like image 3
Mick Avatar answered Nov 16 '22 19:11

Mick