# i swear i'm "special" somtimes.

• 05-26-2008
Terran
i swear i'm "special" somtimes.
what's the best way to get something to happen every say, 60 seconds? i've been trying with
Code:

`  if ((clock () /  CLOCKS_PER_SEC) % 60 == 0){`
from time.h but i'm not sure if that's the best way to do it.
• 05-26-2008
Salem
You're right, it isn't.

clock() measures CPU time, not wall-clock time, so unless you're thrashing the CPU, that's gonna lag.
• 05-26-2008
Terran
Well i was trying to use
Code:

`time_t  start, end;`
, and then something like
Code:

``` while(true) {                 end = Not sure what to put here.                 if ((end - start) ==0){                         //do somthing                 } }```
• 05-26-2008
anon
Look up difftime

And you probably want to compare "now" (time(0)) with start to see if the difference is (greater or ?) equal to 60.0.
• 05-26-2008
matsp
Or if you want other applications to run whilst you are "waiting", use something like Sleep() [windows, 60000 will make 1 minute] or sleep() [Linux/Unix etc, 60 will make one minute].

The OS will then take care of figuring out the time it needs to wake up again. It will be fairly precise, no less so than what you get if you spin in user-mode. The main difference is that if you spin in user-mode, the OS may think "CPU-intensive task", and put your task at a lower priority, whilst a "waiting" task will get raised priority. So it's likely to be MORE precise.

--
Mats
• 05-26-2008
Terran
ahh got it;

Code:

```    const int TimeElapse = 10; // amount of time to wait, in seconds. time_t now = time(0); // set the now time.     while(true)         if ((time(0)-now) &#37; TimeElapse == 0) //comepare now to the current time, and modulus for zero.             cout << time(0) << endl; // test std::cout```
obviously that's not exactly the code i'll use, but it shows that the if works.
• 05-27-2008
iMalc
You better hope nobody adjusts their computer clock whilst that code's running!
Also, if there is a momentary delay such as when a page of virtual memory is loaded from the page file, then you could miss one of your periodic tasks.
You must not look for a specific time. Instead, you'd wait until the time has been reached (or exceeded) and then remember that you have done the task for that minute and wait for the next one.

The best way to do a periodic task really depends on the operating system (please state it).
In a Windows app, using a timer to generate WM_TIMER messages is a good approach, for example.
One could instead use GetTickCount() with code similiar to what you're posted, to avoid problems if your clock is changed.
• 05-27-2008
Elysia
I have a non CPU-eating function (that you could modify) that can be used to sleep or lock to a certain FPS, so to speak.
http://cboard.cprogramming.com/showp...91&postcount=1
If you're interested.
• 05-27-2008
manav
Title of your thread is a little too unobviously clear to me :confused:
• 05-27-2008
Sander
If you really care about correctness, use difftime(). time_t is not guaranteed to be in seconds (although it almost always is).

It is not considered very nice to perform "busy waiting", but like matsp pointed out, the way to perform a "sleep" is unfortunately (but understandibly) not standard across platforms.

--
Computer Programming: An Introduction for the Scientifically Inclined
• 05-27-2008
Terran
it's not like just eating time though. Say i want the user to still be able to use the menu functions while the program is still processing actions in the back and a certain action (say population of a planet) doubles every minute.
• 05-27-2008
Elysia
This calls for a threaded approach. One threads runs a message loop and blocks until messages are received and one thread performs all the other work you want.
This will keep the interface smooth while you can still sleep/wait in the other thread.
• 05-27-2008
Terran
guess i have to learn how to sew then!
• 05-27-2008
iMalc
We still don't know what OS yet.
• 05-27-2008
Terran
Oh, well Vista for the current sys i program on.