/* gcc thisfile.c -lpthread */ #include #include #define NUM_THREADS 5 #define SLEEP_TIME 3 void *sleeping(void *); /* thread routine */ int main(int argc, char *argv[]) { int i; pthread_t tid[NUM_THREADS]; /* array of thread IDs */ for ( i = 0; i < NUM_THREADS; i++) pthread_create(&tid[i], NULL, sleeping, (void *)SLEEP_TIME); for ( i = 0; i < NUM_THREADS; i++) pthread_join(tid[i], NULL); printf("main() reporting that all %d threads have terminated\n", i); return (0); } /* main */ void * sleeping(void *arg) { int sleep_time = (int)arg; printf("thread %ld sleeping %d seconds ...\n", pthread_self(), sleep_time); sleep(sleep_time); printf("\nthread %ld awakening\n", pthread_self()); return (NULL); }