tags:

views:

35

answers:

2

i am using sleep function in ansi-c that gives a resolution up-to milliseconds but not exactly i faced uncertain delays, people suggest me to use windows media timer,

i want to know the comparison between both is they enough reliable to use in real time system or to use some thing else,

thanx in Advance.

+1  A: 

I developed this function for Linux and MacOSX machines. I hope it may help you.

// t => mili seconds

void Wait(unsigned long t)
{
#if defined(_LINUX) || defined(__APPLE__)
    time_t secs;
    long   milisecs,
           nanosecs;
    struct timespec waittime;

    secs     = t / 1000L;
    milisecs = t - (secs * 1000L);
    nanosecs = milisecs * 1000000L;
    waittime.tv_sec   = secs;
    waittime.tv_nsec  = nanosecs;

    nanosleep(&waittime, NULL);
#else      
    struct timeval timeout;

    timeout.tv_sec  = 0;
    timeout.tv_usec = t * 1000L;

    select(0, NULL, NULL, NULL, &timeout);
#endif
}
Jorg B Jorge
I am curious how you learned that select() is more accurate than the native Win32 Sleep(). Is that by experimentation or is this documented somewhere?
jdv
I got the ideia by reading some developers forums.
Jorg B Jorge
A: 

The sleep function is not in the ANSI C standard; if you're talking about the sleep function (with lowercase s), it's the POSIX function, which still has a resolution of 1 second. Instead, probably you're talking about the Windows API Sleep function (uppercase S), which has 1 ms resolution, but that isn't intended to be used when high precision is needed.

If you are on Windows, the suggestion that has been given to you is correct, you should use multimedia timers, that are specifically designed for soft-realtime needs such as multimedia playback.

Matteo Italia