9 static pthread_cond_t cond1
= PTHREAD_COND_INITIALIZER
;
10 static pthread_mutex_t mut1
= PTHREAD_MUTEX_INITIALIZER
;
12 static pthread_cond_t cond2
= PTHREAD_COND_INITIALIZER
;
13 static pthread_mutex_t mut2
= PTHREAD_MUTEX_INITIALIZER
;
15 static bool last_round
;
23 pthread_mutex_lock (&mut1
);
27 if (atomic_decrement_and_test (&ntogo
))
29 pthread_mutex_lock (&mut2
);
31 pthread_cond_signal (&cond2
);
32 pthread_mutex_unlock (&mut2
);
35 pthread_cond_wait (&cond1
, &mut1
);
39 pthread_mutex_unlock (&mut1
);
46 main (int argc
, char *argv
[])
52 bool keeplock
= false;
54 while ((opt
= getopt (argc
, argv
, "n:r:k")) != -1)
58 nthreads
= atol (optarg
);
61 nrounds
= atol (optarg
);
70 pthread_t th
[nthreads
];
72 for (i
= 0; __builtin_expect (i
< nthreads
, 1); ++i
)
73 if (__glibc_unlikely ((err
= pthread_create (&th
[i
], NULL
, cons
, (void *) (long) i
)) != 0))
74 printf ("pthread_create: %s\n", strerror (err
));
76 for (i
= 0; __builtin_expect (i
< nrounds
, 1); ++i
)
78 pthread_mutex_lock (&mut2
);
80 pthread_cond_wait (&cond2
, &mut2
);
81 pthread_mutex_unlock (&mut2
);
83 pthread_mutex_lock (&mut1
);
85 pthread_mutex_unlock (&mut1
);
92 pthread_cond_broadcast (&cond1
);
95 pthread_mutex_unlock (&mut1
);
98 for (i
= 0; i
< nthreads
; ++i
)
99 if ((err
= pthread_join (th
[i
], NULL
)) != 0)
100 printf ("pthread_create: %s\n", strerror (err
));