/* simple program illustrating passing an argument to a thread gcc simple.c -lpthread a.out numWorkers */ #include #include #define SHARED 1 #define MAXWORKERS 100 void *Worker(void *); /* read command line, initialize, and create threads */ int main(int argc, char *argv[]) { int i, numWorkers; pthread_attr_t attr; pthread_t workerid[MAXWORKERS]; /* set global thread attributes */ pthread_attr_init(&attr); pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); /* read command line */ numWorkers = atoi(argv[1]); /* create the workers, then exit */ for (i = 0; i < numWorkers; i++) pthread_create(&workerid[i], &attr, Worker, (void *) i); pthread_exit(NULL); } /* Each worker prints its identity then quits */ void *Worker(void *arg) { int myid = (int) arg; printf("worker %d (pthread id %d) has started\n", myid, pthread_self()); }