/* * Exemplo para revisão de create e join. As threads executarão o * suposto trabalho duro de forma paralela ou sequencial? */ #include #include #include #include #define N_THR 10 void* f_thread(void *v) { int thr_id = (int) v; pthread_t thr; printf("Thread %d iniciou execução.\n", thr_id); if (v + 1 < N_THR) pthread_create(&thr, NULL, f_thread, (void*) v + 1); /* Suponha muito trabalho aqui !!! */ if (v + 1 < N_THR) pthread_join(thr, NULL); printf("Thread %d encerrou execução.\n", thr_id); return NULL; } int main() { pthread_t thr; pthread_create(&thr, NULL, f_thread, (void*) 0); /* Suponha muito trabalho aqui !!! */ pthread_join(thr, NULL); return 0; }