#include #include #include #define NUM_THREADS 5 void *printNtimes(int *number) { int n=*number, i; for (i=0; i < n; i++) { printf("I'm a thread that prints %d %d times. Go me!\n", n, n); } free(number); pthread_exit(NULL); } int main(int argc, char *argv[]) { /* allocate space for 5 threads */ pthread_t threads[NUM_THREADS]; int *n, p, i; for (i = 0; i < NUM_THREADS; i++) { /* ask for a number */ printf("Please enter a number: \n"); /* we want the arguments to be an int */ n = (int *)malloc(sizeof(int)); /* convert character to an int */ *n = getchar() - '0'; p = pthread_create(&threads[i], NULL, printNtimes, n); if (p) { fprintf(stderr, "ERROR: pthread_create() not happy\n"); exit(-1); } } for (i = 0; i < NUM_THREADS; i++) { pthread_join(threads[i], NULL); } return 0; }