I have some source code that was compiled on Windows. I am converting it to run on Red Hat Linux.
The source code has included the <windows.h>
header file and the programmer has used the Sleep()
function to wait for a period of milliseconds. This won't work on the Linux.
However, I can use the sleep(seconds)
function, but that uses integer in seconds. I don't want to convert milliseconds to seconds. Is there a alternative sleep function that I can use with gcc compiling on Linux?
Best Answer
Yes - older POSIX standards defined
usleep()
, so this is available on Linux:usleep()
takes microseconds, so you will have to multiply the input by 1000 in order to sleep in milliseconds.usleep()
has since been deprecated and subsequently removed from POSIX; for new code,nanosleep()
is preferred:An example
msleep()
function implemented usingnanosleep()
, continuing the sleep if it is interrupted by a signal: