Question about threading implementation

Andrew Hacking ahacking at optusnet.com.au
Mon Mar 7 17:33:15 PST 2005


On Mon, 2005-03-07 at 22:24, Jonathon McKitrick wrote: 
> On Mon, Mar 07, 2005 at 11:43:53AM +1000, Andrew Hacking wrote:
> : Calling sleep() in a multi-threaded program is probably not safe since
> : it suspends the process, not just the thread.
> : You should always call nanosleep() in multi-threaded apps.
> 
> Are you sure nanosleep() doesn't suspend the process?

As sure as I can be. ;-)

I remember experiencing a number of problems with sleep() on at least
one or more unix variants when using threads.
However I have used nanosleep() successfully on FreeBSD, Solaris, Linux,
AIX and HP-UX.

The FreeBSD implementation of sleep() is based on nanosleep() [according
to the man page] so I was probably wrong to say don't use sleep() in
your particular case,  there should be nothing to worry about on
FreeBSD/DFly.  However if you prefer portable thread-safe code,
nanosleep() is the best sleeping variant to use. On many unix variants
sleep() will not work correctly when using threads because the
implementation of sleep() often relies on signals.

-Andrew





More information about the Users mailing list