#include #include #include #include #define MAX_THREAD 10 typedef struct { int* nb_threads; } paramStruct; // data structure will be passed to a thread // function that a thread executes. void* thread_handler(void* param) { paramStruct * p = (paramStruct *) param; //get parameters pthread_t tID = pthread_self(); printf("Staring thread tid %lu, current nb_thread=%d\n", (unsigned long int) tID, *p->nb_threads); //Do something here sleep(5);//sleep for 50 seconds printf("End of thread tid %lu, current nb_thread=%d\n", (unsigned long int) tID, *p->nb_threads); return NULL; } int main(int argc, char **argv) { int nb_threads=0; pthread_t threadList [MAX_THREAD]; //list of threads, for later cleaning them. for (int i=0; i<10; i++) { nb_threads++; // count nb of thread created // Generate a thread to execute a task in thread_handler with paramet in client_info pthread_t* t_child =malloc(sizeof(pthread_t)); paramStruct* param = malloc(sizeof(paramStruct)); // must do it to make sur each thread receive different memory for param param->nb_threads =&nb_threads; pthread_create(t_child, NULL, thread_handler, param ); threadList[i]=*t_child; } // cleaning the stacks of all threads. for (int i=0; i<10; i++) { pthread_join(threadList[i], NULL); } }