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?
You can use this cross-platform function:
Alternatively to
usleep()
, which is not defined in POSIX 2008 (though it was defined up to POSIX 2004, and it is evidently available on Linux and other platforms with a history of POSIX compliance), the POSIX 2008 standard definesnanosleep()
: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:Beyond usleep, the humble select with NULL file descriptor sets will let you pause with microsecond precision, and without the risk of
SIGALRM
complications.sigtimedwait and sigwaitinfo offer similar behavior.