Out of curiosity, how do you write a program that takes an action every second? Or every half second?
Do you just get access to the system time and run a while(1) loop and keep on checking?
Thanks.
Printable View
Out of curiosity, how do you write a program that takes an action every second? Or every half second?
Do you just get access to the system time and run a while(1) loop and keep on checking?
Thanks.
Yes and no, it's not a good idea to stay in a loop wasting the CPU.
At least two common ways I know of,
1. use sleep() (unix) or Sleep() (Win32)
2. Use some sort of timer API provided by the system (Ie, Windows Timers)
Note also that you may want to sleep for less than 1 second, otherwise you may find that you drift [1]. Also, if you are showing the time in some form, make sure you ask the OS what time it each time you update the time. Sleeping for 0.1s (100 milliseconds) and checking to see if the time (in seconds) has changed would do the task.
The extra overhead shouldn't make much of a difference.
[1] Since the OS will guarantee that it sleeps of AT LEAST the time specified, and you may find that it sleeps for, say, 10 milliseconds more than what you specified. If you then do some processing (such as writing the time to a window or console display), and then sleep again, you will eventually get out of sync with the actual time.
--
Mats
Who said he was talking about Windows?
Quote:
Originally Posted by man sleep
Ok, fair point, it should say "May not sleep the exact amount you asked for". And even if the sleep operation itself _IS_ precise, the scheduler in the OS may well think that there are other more important tasks to run at the time when the process becomes runnable, so it may not be run until "some time later" - so it's fine to sleep, but relying on the amount of time it actually sleeps is not going to work, whether it rounds the sleep time down or up, and whether your process is the only one or not, it will sooner or later drift out of sync if you rely on the timing from operations that put the process to sleep. You still need to read the system time (at least every now and again - and it's just more logic to do it often, and system time is one of the functions in the OS that is highly optimized).
--
Mats
Elysia, can I have you code that properly sets a timer, pretty please with sugar on top? (unless you are a diabetic or on a diet, then you get the artificial sweetener, unless you are into health food and aren't into the long term effects of poisons in your body, then I'll just put an apple on top, but be sure to peel it before eating so you don't inject the 27+ pesticides found in the skin...)
Originally for C++, this should allow it to work for C:
Basically the arguments are - the time you wish it to tun and how many times in that time period you want it to callback [in ms] (eg, 1000, 60 - 60 times per second).Code:typedef void (Callback*)();
void Synchronize(const UINT64 dwTimeFrame, const UINT64 dwFramesPerTimeFrame, Callback* pCallback)//CCallback& rCallback)
{
const UINT64 dwTickRate = dwTimeFrame / dwFramesPerTimeFrame; // Calculate amount of time to sleep each time
INT32 nSleepNeeded; // Stores the amount of time we need to sleep
UINT64 dwTimeElapsed; // Time elapsed since beginning of time frame
UINT64 dwNextSleepBoundary; // The next "target" time to sleep until.
DWORD dwTick; // Used to hold a snapshot of timeGetTime so we can calculate elapsed time
DWORD dwTick2 = timeGetTime(); // Snapshot of the beginning of the code sample
// Initialize variables
nSleepNeeded = 0;
dwTimeElapsed = 0;
dwNextSleepBoundary = dwTickRate; // Set the next sleep boundary to the next tick.
dwTick = timeGetTime(); // Get a snapshot of the current time
while (dwNextSleepBoundary <= dwTimeFrame)
{
//rCallback.DoCallback();
pCallback();
dwTimeElapsed = timeGetTime() - dwTick; // Calculate elapsed time
INT64 nTemp = dwNextSleepBoundary - dwTimeElapsed; // Calculate amount of time we need to sleep (at least)
ASSERT(nTemp <= 0xFFFFFFFF); // Must not exceed the precision of a UINT32
if (nTemp > 0) // Safesty check; we don't want to do Sleep(0) or sleep a negative value since Sleep takes a DWORD
{
nSleepNeeded = (UINT32)nTemp;
Sleep(nSleepNeeded); // Sleep for the amount of time necessary before next drawing
}
dwNextSleepBoundary += dwTickRate; // Increase sleep "boundary" to next time frame tick
}
}
This is for Windows, we have yet to hear what OS the OP is using.
Remember that mats used the word "OS", but Windows's Sleep function does not sleep for at least, but rather approximately the amount specified. Or in other words, don't rely on the sleep function to sleep at least the number of time units specified; instead count on it sleeping approximately the number of time units specified.
Thank You!!