12

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
  • 3,638
  • 8
  • 47
  • 84

2 Answers2

17

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.

cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • 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. – bgoodr Oct 09 '15 at 02:01
7

Just the thread which calls the function.

jalf
  • 243,077
  • 51
  • 345
  • 550
  • Have you ever test for that? In my test program, it seems to make main thread sleep. – Nick Dong Aug 11 '12 at 14:40
  • Yes, I have tested it. I've also read the documentation for the functions. – jalf Aug 11 '12 at 15:26
  • @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. – Martin James Aug 11 '12 at 19:10
  • Could you post your test program here? I post mine above, but I am confusing with the result. – Nick Dong Aug 12 '12 at 04:18