Implementing time delay function in C

I want to implement a delay feature using empty loops. But the time it takes to complete the loop is compiler and machine dependent. I want my program to independently determine the time and delay the program for a specified period of time. Can anyone give me an idea how to do this? NB There is a function called delay () that pauses the system for specified milliseconds. Is it possible to suspend the system without using this function?

+3


source to share


3 answers


First of all, you should never sit in a loop doing nothing. Not only is it wasted energy (since it keeps your CPU 100% busy by counting your cycle counter) - on a multitasking system, it also reduces overall system performance because your process is constantly getting time slices like it seems to be doing something- then.

Next point ... I don't know any function delay()

. This is not a C standard. In fact, before C11, there was no standard for such things.

POSIX to the Rescue, usleep(3)

(deprecated) and nanosleep(2)

. If you are on a POSIX-compatible system, you should be fine. They block (meaning your OS's scheduler knows they have nothing to do and only schedules them after the call ends) so you don't waste any CPU power.



If you are on windows, for direct code delay you have Sleep()

. Note that this function takes milliseconds, but usually only matters around 15ms. Often good enough, but not always. If you need higher precision on windows, you can request more timer interrupts with timeBeginPeriod()

... timeBeginPeriod(1);

will request a timer interrupt every milliseconds.Don't forget to call timeEndPeriod()

with the same value once you no longer need precision, as more timer interrupts come with a cost: they make the system work, thereby wasting more energy.

I had a somewhat similar problem developing a small game lately, I need constant ticks after 10ms, this is what I came up with for POSIX compatible systems and for windows . The function ticker_wait()

in this code just pauses until the next tick, perhaps useful if your original intent was a timing issue.

+3


source


If you are not using a real-time operating system, anything you program directly will not be accurate. You need to use the system function to sleep for some time, such usleep

as in Linux or Sleep

Windows.

Since the operating system can interrupt the process earlier or later than the expected time, you should get the system time before and after sleep to determine how long you slept.

Edit:



On Linux, you can get the current system time from gettimeofday

, which has a resolution in microseconds (that the actual clock is accurate is a different story). On Windows, you can do something like this with GetSystemTimeAsFileTime

:

int gettimeofday(struct timeval *tv, struct timezone *tz)
{
    const unsigned __int64 epoch_diff = 11644473600000000;
    unsigned __int64 tmp;
    FILETIME t;

    if (tv) {
        GetSystemTimeAsFileTime(&t);

        tmp = 0;
        tmp |= t.dwHighDateTime;
        tmp <<= 32;
        tmp |= t.dwLowDateTime;

        tmp /= 10;
        tmp -= epoch_diff;
        tv->tv_sec = (long)(tmp / 1000000);
        tv->tv_usec = (long)(tmp % 1000000);
    }

    return 0;
}

      

+2


source


You can do something like finding the exact time it is at a particular point in time and then store it in a while loop that rereads the time until it reaches what you want. Then it just breaks out and continues with the rest of your program. I'm not sure if I see a lot of benefit in the loop, not just using the delay function.

+1


source







All Articles