[PD] usleep() equivalent to Sleep() ?

jdavison at speakeasy.org jdavison at speakeasy.org
Mon May 13 18:32:00 CEST 2002


        My following remarks are UNIX-centric; for the purposes of this
discussion, I equate UNIX with the Open Group Base Specifications Issue 6/IEEE
Std 1003.1-2001.  See URL
"http://www.opengroup.org/onlinepubs/007904975/nfindex.html" for more
information.

        Martin Pi writes:

>but it is recommended to use select() with NULLs and a timeval struct instead,
>i was told once.

        That's stale advice from pre-nanosleep/microsleep days.  In UNIX and
UNIX-like systems, struct timeval provides microsecond resolution, and struct
timespec provides nanosecond resolution.  Therefore, other things equal, it's
preferable to use a routine that uses struct timespec.  Such routines are
"clock_nanosleep" (see URL
"http://www.opengroup.org/onlinepubs/007904975/functions/clock_nanosleep.html"),
"nanosleep" (see URL
"http://www.opengroup.org/onlinepubs/007904975/functions/nanosleep.html"),
"pselect" (see URL
"http://www.opengroup.org/onlinepubs/007904975/functions/select.html"), and
"pthread_cond_timedwait" (see URL
"http://www.opengroup.org/onlinepubs/007904975/functions/pthread_cond_timedwait.html").

        Not all UNIX or UNIX-like systems provide nontrivial implementations of
these routines, and some of the routines ("clock_nanosleep", "pselect") are new
enough that the routines may not even exist in stub form.  The usual
compile-time and run-time checks must be done.  For example, Darwin Version 5.4
in MacOS X 10.1.3 is lagging badly; it doesn't even have "nanosleep".  One
hopes that MacOS X 10.2 will bring us a version of Darwin that is at least up
to 1998 standards.  (The 1998 Single UNIX Specification, Version 2 had
nanosleep -- see URLs "http://www.opengroup.org/onlinepubs/007908799/toc.htm"
and "http://www.opengroup.org/onlinepubs/007908799/xsh/nanosleep.html".)

        Guenter Geiger writes:

>I recommend using sys_microsleep from m_imp.h, ...

        I can't speak for Microsoft Windows, but to the best of my knowledge,
"sys_microsleep" and <m_imp.h> are vendor-specific; neither exists in the Open
Group Base Specifications Issue 6/IEEE Std 1003.1-2001 (or any of its
predecessors), SunOS 5.8, or Darwin Version 5.4.  If a system has neither of
these, a wrapper routine ought to exist which, based on compile-time and
run-time checks, falls back on one of the alternatives listed above.  (For
example, with Darwin Version 5.4, one might fall back on
"pthread_cond_timedwait", "usleep", or "select".)

-- 
John Davison, jdavison at-sign speakeasy period org



More information about the Pd-list mailing list