Am I programming with pthread on linux (Centos)? I want the streams to sleep for a while to wait for something. I am trying to use sleep (), nanosleep () or usleep (), or something can do it. I want to ask: Are sleeping functions sleeping on all topics or only the one who calls it? Any advice or recommendations would be appreciated.
void *start_routine () { sleep (1); ... } int main (int argc, char **argv) { 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
source share