/* cc thisfile.c -lthread -lpthread */ #include #include #define NUM_THREADS 5 void *work(void *); /* thread routine */ int v = 0; /* global variable */ 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, work, NULL); for ( i = 0; i < NUM_THREADS; i++) pthread_join(tid[i], NULL); printf("main() reporting that all %d threads have terminated\n", i); printf("v should be %d, it is %d\n", NUM_THREADS, v); return (0); } /* main */ void * work(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()); v ++; return (NULL); }