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.

Share:
91,131

Related videos on Youtube

Steveng
Author by

Steveng

Updated on July 09, 2022

Comments

  • Steveng
    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 calling sleep?

  • user48956
    user48956 about 12 years
    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.h. Is this documentation wrong or is there a second sleep function?
  • caf
    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
    damianostre over 7 years
    Yep, that's easier to use than nanosleep in most cases.
  • dpi
    dpi about 6 years
    Note 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
    Will Eccles almost 4 years
    This is inaccurate. The sleep() will cause the current thread to sleep. See sleep(3). Lots of older man pages still use the word "process," but in reality a thread is a process.