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 make sure to hear some feedback from other people as well.
The sleep() function shall cause the calling thread to be suspended from execution... Here's what "man 3 sleep" says: "sleep() makes the calling process sleep until seconds seconds have elapsed..." (Ubuntu 10). The function is in unistd.
Explanation: When you want to sleep a thread, condition variable can be used. In C under Linux, there is a function pthread_cond_wait() to wait or sleep. On the other hand, there is a function pthread_cond_signal() to wake up sleeping or waiting thread. Threads can wait on a condition variable.
The function sleep gives a simple way to make the program wait for a short interval. If your program doesn't use signals (except to terminate), then you can expect sleep to wait reliably throughout the specified interval.
We can wake the thread by calling either the notify() or notifyAll() methods on the monitor that is being waited on. Use notifyAll() instead of notify() when you want to wake all threads that are in the waiting state.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With