tags:

views:

243

answers:

4

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

+7  A: 

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.

unwind
+1  A: 

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

Mick Sharpe
+1  A: 

If your application runs on Windows, instead, you can use the SetTimer function.

Matteo Italia
A: 

Agree with unwind's recommendation for alarm() but don't forget to set another alarm() after you are done with the periodic processing block.

Suresh Krishnan