Do sleep functions sleep all threads or just the one who call it?

42,250

Solution 1

The standard spells it:

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

The linux one is just as clear:

sleep() makes the calling thread sleep until...

There are however a few erroneous references which maintain otherwise. linux.die.net used to state sleep causes the process to wait.

Solution 2

Just the thread which calls the function.

Share:
42,250
Nick Dong
Author by

Nick Dong

Updated on July 05, 2022

Comments

  • Nick Dong
    Nick Dong almost 2 years

    I am programming with pthread on linux(Centos)? I wanna to threads sleep a short time to wait for something. I am trying to use sleep(), nanosleep(), or usleep() or maybe something can do that. I want to ask that: Do sleep functions sleep all threads or just the one who call it? Any advices or references would be appreciate.

    void *start_routine () {
        /* I just call sleep functions here */
        sleep (1); /* sleep all threads or just the one who call it? 
                      what about nanosleep(), usleep(), actually I 
                      want the threads who call sleep function can 
                      sleep with micro-seconds or mili-seconds.  
                   */
        ...
    }
    
    int main (int argc, char **argv) {
        /* I just create threads here */
        pthread_create (... ...);
        ...
        return 0;
    }
    

    My test program:

    #define _GNU_SOURCE
    #include <pthread.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <sched.h>
    #include <unistd.h>
    
    void *start_routine (void *j) {
    
        unsigned long sum;
        int i;
        int jj;
        jj = (int)j;
        do {
            sum = 1;
            for (i=0; i<10000000; i++) {
                sum = sum * (sum+i);
            }
            if (jj == 0) {
                printf ("\033[22;33m[jj%d.%ld]\t", jj, sum);
                sleep(1);           
            }
            else {
                printf ("\033[22;34m[jj%d.%ld]\t", jj, sum);
            }
    
        }while (1);
    
        pthread_exit((void *)0);
    }
    int main(int argc, char *argv[])
    {
        cpu_set_t cpuset;
        pthread_t thread[2];
        int i;
        i = 0;
        CPU_ZERO(&cpuset);
        CPU_SET(i, &cpuset);
    
        pthread_create (&thread[0], NULL, start_routine, (void *)i);
        pthread_setaffinity_np(thread[0], sizeof(cpu_set_t), &cpuset);
        i = 1;
        CPU_ZERO(&cpuset);
        CPU_SET(i, &cpuset);
        pthread_create (&thread[1], NULL, start_routine, (void *)i);
        pthread_setaffinity_np(thread[1], sizeof(cpu_set_t), &cpuset);
        pthread_exit (NULL);
    }
    
  • Nick Dong
    Nick Dong over 11 years
    Have you ever test for that? In my test program, it seems to make main thread sleep.
  • josesuero
    josesuero over 11 years
    Yes, I have tested it. I've also read the documentation for the functions.
  • Martin James
    Martin James over 11 years
    @jalf is 100% correct. If the Sleep() call is making the main thread sleep, then the main thread is directly calling it or is waiting on some other signal that is only supplied by the sleeping thread after the sleep.
  • Nick Dong
    Nick Dong over 11 years
    Could you post your test program here? I post mine above, but I am confusing with the result.
  • bgoodr
    bgoodr over 8 years
    I found that m Red Hat Enterprise Linux Workstation release 6.6 system has a man page for sleep(3) that reads "sleep() makes the calling process sleep until..." so maybe the upstream man pages have been updated.