#include <unistd.h>
#include <pthread.h>
#include <stdio.h>
#include <time.h>
#define NUM_THREADS 10
void *thread_function(void *arg) {
fprintf(stdout, "Thread: %d running.\n", (int)arg);
sleep((int)arg+1);
fprintf(stdout, "Thread: %d done.\n", (int)arg);
pthread_exit(0);
}
int main() {
int cnt;
pthread_t p_thread[NUM_THREADS];
for(cnt = 0; cnt < NUM_THREADS; cnt++)
if(pthread_create(&p_thread[cnt], NULL, thread_function, (void *)cnt) != 0)
fprintf(stderr, "Error creating the thread");
for(cnt = 0; cnt < NUM_THREADS; cnt++) {
pthread_join(p_thread[cnt], NULL);
}
fprintf(stdout, "All threads completed.\n");
return 0;
}