//compile gcc -o first.exe -pthread firstcode.c #include #include #include #define NUM_THREADS 6 /* create thread argument struct for thr_func() */ typedef struct _thread_data_t { int tid; double stuff; } thread_data_t; /* thread function */ void *thr_func(void *arg) { thread_data_t *data = (thread_data_t *)arg; printf("hello from thr_func, thread id: %d\n", data->tid); pthread_exit(NULL); } int main(int argc, char **argv) { pthread_t thr[NUM_THREADS]; int i; /* create a thread_data_t argument array */ thread_data_t thr_data[NUM_THREADS]; /* create threads */ for (i = 0; i < NUM_THREADS; ++i) { thr_data[i].tid = i; if ((pthread_create(&thr[i], NULL, thr_func, &thr_data[i])) != 0) { fprintf(stderr, "error: pthread_create\n "); return EXIT_FAILURE; } } /* block until all threads complete */ for (i = 0; i < NUM_THREADS; ++i) { pthread_join(thr[i], NULL); } return EXIT_SUCCESS; }