what's the best way to get something to happen every say, 60 seconds? i've been trying with
from time.h but i'm not sure if that's the best way to do it.Code:if ((clock () / CLOCKS_PER_SEC) % 60 == 0){
what's the best way to get something to happen every say, 60 seconds? i've been trying with
from time.h but i'm not sure if that's the best way to do it.Code:if ((clock () / CLOCKS_PER_SEC) % 60 == 0){
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.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Well i was trying to use, and then something likeCode:time_t start, end;Code:while(true) { end = Not sure what to put here. if ((end - start) ==0){ //do somthing } }
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.
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
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
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
ahh got it;
obviously that's not exactly the code i'll use, but it shows that the if works.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) % TimeElapse == 0) //comepare now to the current time, and modulus for zero. cout << time(0) << endl; // test std::cout
Last edited by Terran; 05-26-2008 at 04:23 PM.
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.
Last edited by iMalc; 05-27-2008 at 01:07 AM.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
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.
Title of your thread is a little too unobviously clear to me
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
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.
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.
guess i have to learn how to sew then!
We still don't know what OS yet.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
Oh, well Vista for the current sys i program on.