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);
}
Python time sleep function is used to add delay in the execution of a program. We can use python sleep function to halt the execution of the program for given time in seconds. Notice that python time sleep function actually stops the execution of current thread only, not the whole program.
The sleep function waits for seconds seconds or until a signal is delivered, whichever happens first. If sleep returns because the requested interval is over, it returns a value of zero. If it returns because of delivery of a signal, its return value is the remaining time in the sleep interval.
Wait() method releases lock during Synchronization. Sleep() method does not release the lock on object during Synchronization. Wait() should be called only from Synchronized context. There is no need to call sleep() from Synchronized context.
sleep() method can be used to pause the execution of current thread for specified time in milliseconds. The argument value for milliseconds can't be negative, else it throws IllegalArgumentException .
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.
Just the thread which calls the function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With