#include <stdio.h>#include <stdlib.h>#include <pthread.h>#include <signal.h>#include <unistd.h>#define NUM_THREADS 5000volatile int while_condition = 1;void sig_handler(int signal){ while_condition = 0;}void *do_work(void *ptr) { size_t i = (size_t)ptr; while (while_condition) { sleep(1); } pthread_exit(NULL);}int main(int argc, char** argv) { pthread_t threads[NUM_THREADS]; pthread_attr_t attr; void *ptr; size_t i; signal(SIGINT, sig_handler); /* Initialize and set thread detached attribute */ pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); for (i = 0; i < NUM_THREADS; ++i) { printf("In main: creating thread %ld\n", i); int rc = pthread_create(&threads[i], &attr, do_work, (void *)i); if (rc) { printf("ERROR; return code from pthread_create() is %d\n", rc); exit(-1); } } /* Free attribute and wait for the other threads */ pthread_attr_destroy(&attr); for (i = 0; i < NUM_THREADS; ++i) { int rc = pthread_join(threads[i], &ptr); if (rc) { printf("ERROR; return code from pthread_join() is %d\n", rc); exit(-1); } printf("In main: stopped thread %ld\n", i); } return (EXIT_SUCCESS);}