/* ================================================================= Thread example: thread creation (with input parameter) ================================================================= */ #include #include #include void *worker(void *arg) { int x; x = * (int *) arg; cout << "Hi, my input parameter is " << x << endl; return(NULL); /* Thread exits (dies) */ } /* ======================= MAIN ======================= */ int main(int argc, char *argv[]) { int i, num_threads; pthread_t tid[100]; /* Thread ID used for thread_join() */ int param[100]; /* used to store parameters for threads */ pthread_attr_t attr; pthread_attr_init(&attr); /* ----- Check command line ----- */ if ( argc != 2 ) { cout << "Usage: " << argv[0] << " Num_Threads" << endl; exit(1); } /* ----- Get number of intervals and number of threads ----- */ num_threads = atoi(argv[1]); /* ------ Create threads ------ */ for (i = 0; i < num_threads; i = i + 1) { param[i] = 1001 + i; if ( pthread_create(&tid[i], &attr, worker, ¶m[i]) ) { cout << "Cannot create thread" << endl; exit(1); } } for (i = 0; i < num_threads; i = i + 1) pthread_join(tid[i], NULL); }