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
2010-01-12 15:06:39
+1
A:
You will need to create a thread or process that runs a loop containing a wait request.
Mick Sharpe
2010-01-12 15:06:56
+1
A:
If your application runs on Windows, instead, you can use the SetTimer function.
Matteo Italia
2010-01-12 16:12:26
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
2010-01-12 22:36:20