static pthread_barrier_t threads_started_barrier;
+static pthread_barrier_t threads_started_barrier2;
+
#define NUM 15
static int num_threads = NUM;
pthread_barrier_init (&threads_started_barrier, NULL, NUM + 1);
+ pthread_barrier_init (&threads_started_barrier2, NULL, 2);
+
for (i = 0; i < NUM; i++)
{
res = pthread_create (&threads[i],
pthread_barrier_wait (&threads_started_barrier);
- sleep (180); /* all threads started */
+ pthread_barrier_wait (&threads_started_barrier2); /* all threads started */
+
+ pthread_join (threads[0], NULL);
+
+ sleep (180); /* first child thread exited */
exit (EXIT_SUCCESS);
}
pthread_barrier_wait (&threads_started_barrier);
- /* Don't run forever. Run just short of it :) */
- while (shared_var > 0)
+ if (my_number > 0)
{
- shared_var++;
- usleep (1); /* Loop increment. */
- loop ();
+ /* Don't run forever. Run just short of it :) */
+ while (shared_var > 0)
+ {
+ shared_var++;
+ usleep (1); /* Loop increment. */
+ loop ();
+ }
}
+ else
+ pthread_barrier_wait (&threads_started_barrier2);
pthread_exit (NULL);
}