开发者

Does calling sleep() from pthread put thread to sleep or process?

开发者 https://www.devze.com 2023-03-09 07:50 出处:网络
I saw that there is a question about pthread sleep linux However, when I looked up the man page on my Linux machine, I see the following:

I saw that there is a question about pthread sleep linux

However, when I looked up the man page on my Linux machine, I see the following:

SYNOPSIS #include <unistd.h>

   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 m开发者_C百科ake sure to hear some feedback from other people as well.


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号