28

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?

lucian.pantelimon
  • 3,673
  • 4
  • 29
  • 46
Steveng
  • 1,141
  • 2
  • 11
  • 14

6 Answers6

49

Just the thread. The POSIX documentation for sleep() says:

The sleep() function shall cause the calling thread to be suspended from execution...

caf
  • 233,326
  • 40
  • 323
  • 462
  • 6
    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? – user48956 May 14 '12 at 17:42
  • 11
    @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. – caf May 15 '12 at 01:24
11

Try this,

#include <unistd.h>

usleep(microseconds);
ANjaNA
  • 1,404
  • 2
  • 16
  • 29
Chand Priyankara
  • 6,739
  • 2
  • 40
  • 63
6

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.

CMorgan
  • 323
  • 3
  • 5
0

In practice, there are few cases where you just want to sleep for a small delay (milliseconds). For Linux, read time(7), and see also this answer. For a delay of more than a second, see sleep(3), for a small delay, see nanosleep(2). (A counter example might be a RasPerryPi running some embedded Linux and driving a robot; in such case you might indeed read from some hardware device every tenth of seconds). Of course what is sleeping is just a single kernel-scheduled task (so a process or thread).

It is likely that you want to code some event loop. In such a case, you probably want something like poll(2) or select(2), or you want to use condition variables (read a Pthread tutorial about pthread_cond_init etc...) associated with mutexes.

Threads are expensive resources (since each needs a call stack, often of a megabyte at least). You should prefer having one or a few event loops instead of having thousands of threads.

If you are coding for Linux, read also Advanced Linux Programming and syscalls(2) and pthreads(7).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
0

Posix sleep function is not thread safe. https://clang.llvm.org/extra/clang-tidy/checks/concurrency/mt-unsafe.html

rjhcnf
  • 762
  • 6
  • 12
-5

sleep() function does not cease a specific thread, but it stops the whole process for the specified amount of time. For stopping the execution of a particular thread, we can use one pthread condition object and use pthread_cond_timedwait() function for making the thread wait for a specific amount of time. Each thread will have its own condition object and it will never receive a signal from any other thread.

Ricky
  • 635
  • 2
  • 5
  • 20
  • 1
    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. – Will Eccles Jun 08 '20 at 15:46