pthread sleep linux
91,131
Solution 1
Just the thread. The POSIX documentation for sleep() says:
The
sleep()
function shall cause the calling thread to be suspended from execution...
Solution 2
Try this,
#include <unistd.h>
usleep(microseconds);
Solution 3
I usually use nanosleep and it works fine. Nanosleep supends the execution of the calling thread. I have had the same doubt because in some man pages sleep refers to the entire process.
Related videos on Youtube
Author by
Steveng
Updated on July 09, 2022Comments
-
Steveng almost 2 years
I am creating a program with multiple threads using pthreads.
Is
sleep()
causing the process (all the threads) to stop executing or just the thread where I am callingsleep
? -
user48956 about 12 yearsHere's what "man 3 sleep" says: "sleep() makes the calling process sleep until seconds seconds have elapsed..." (Ubuntu 10). The function is in unistd.h. Is this documentation wrong or is there a second sleep function?
-
caf about 12 years@user48956: For historical reasons, the man pages often use "process" where the behaviour now applies to the "thread" - this is the case in the
sleep(3)
man page that you refer to. I suggest submitting a bug to Ubuntu about the documentation. -
damianostre over 7 yearsYep, that's easier to use than
nanosleep
in most cases. -
dpi about 6 yearsNote that usleep() is obsolete and has been removed from POSIX.1-2008. You should use either sleep(), or nanosleep() if higher resolution is required.
-
Will Eccles almost 4 yearsThis is inaccurate. The
sleep()
will cause the current thread to sleep. Seesleep(3)
. Lots of older man pages still use the word "process," but in reality a thread is a process.