/* Aaron Linville aaron linville.org http://www.linville.org thread.c A brief example of pthreads. A defined number of threads sleep for a set amount of time and then wakeup. */ #define NUM_THREADS 5 #include #include void *routine(void *); int main(int argc, char *argv) { int i, ret; pthread_t pthread_id[NUM_THREADS]; for(i = 0; i < NUM_THREADS; i++) { ret = pthread_create(pthread_id + i, NULL, routine, (void *) i); if(ret != 0) { fprintf(stderr, "Error creating thread %i. Error %i\n", i, ret); } } for(i = 0; i < NUM_THREADS; i++) { ret = pthread_join(pthread_id[i], NULL); if(ret != 0) { fprintf(stderr, "Error waiting for %i to join. Error %i.\n", i, ret); } } exit(0); } void *routine(void *num) { printf("%i going to sleep.\n", num); sleep(NUM_THREADS - (int) num); printf("%i waking up!\n", num); return; }