#include #include #include #define THREADS 4 /* the function called for each thread */ void* f(void* idp) { int id = * (int*) idp; sleep(1); printf("Thread %d checking in!\n", id); pthread_exit(NULL); } int main() { /* an array of threads */ pthread_t threads[THREADS]; int ids[THREADS]; int i; /* spawn the threads */ for (i = 0; i < THREADS; i++) { ids[i] = i; pthread_create(&threads[i], NULL, f, &ids[i]); } /* wait for the threads to finish */ for (i = 0; i < THREADS; i++) { pthread_join(threads[i], NULL); } printf("All threads finished!\n"); pthread_exit(NULL); }