Does calling sleep() from pthread put thread to sl

2019-04-20 12:18发布

问题:

I saw that there is a question about pthread sleep linux

However, when I looked up man page on my linux machine, I see the following.

SYNOPSIS #include

   unsigned int sleep(unsigned int seconds);

DESCRIPTION sleep() makes the current process sleep until seconds seconds have elapsed or a signal arrives which is not ignored.

So my question is that I would like to know which man page I should follow to put the thread sleep. In addition, if both are true, how can I control that?

I can probably write some code to test it but I want to make sure to hear some feedback from other people as well.

Thank you.

回答1:

The wording in your man page is likely wrong. Trust the standard and trust the man page on kernel.org. Write to the maintainer of the documentation for your distro and tell them to update the manual pages.



回答2:

There are two man pages regarding sleep function on my Linux box:

$ man -k sleep
sleep (3)   - Sleep for the specified number of seconds
sleep (3p)  - suspend execution for an interval of time

The 1st one says "the current process" as does yours.
The 2nd one says "the calling thread" but its preamble states:

This manual page is part of the POSIX Programmer’s Manual. The Linux implementation of this interface may differ (consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux.

So i conclude that sleep(3) describes the actual behaviour and sleep(3p) is only there for reference.



回答3:

The man page referenced by @cnicutar says that sleep is not thread-safe (maybe that's new since 2011?). Interestingly, Dave Butenhof's 1997 book ('Programming with Posix Threads') does include an example which sleeps a thread with sleep (p18). This is an old thread (the other sort) on comp.programming.threads in which Butenhof and others discuss nanosleep in the context of pthreads.

In short, nanosleep is, I think, Ok, but sleep isn't. The nanosleep man page at kernel.org doesn't say whether nanosleep is thread-safe, but the gcc sleeping docs say that it is.



标签: c pthreads sleep