0

I have a question regarding using sleep() in a thread created via pthread_create() on mainline (vanilla) Linux (kernel 2.6.18)

Suppose I create 2 threads whose execution codes are as follows:

thread1(void *){
 while (1) {
    sleep(5);
 }
}

thread2(void *){
 while (1) {
    sleep(9);
 }
}

My question is whether the sleep() in one thread will interfere with the sleep() in another thread, for example, if thread1 wakes up from sleep(), will thread2 be waken prematurely as well?

I tried my code on CentOS 5.4, and it seems the sleep() in each thread is independent of another.

However, I am not sure if this independence is guaranteed. The underlining implementation of sleep() should rely on some signals, say SIGALARM, and I don't know if it is well defined that which thread gets a signal when the signal arrives.

Diosney
  • 10,520
  • 15
  • 66
  • 111

0 Answers0