8 static pthread_cond_t cond1
= PTHREAD_COND_INITIALIZER
;
9 static pthread_mutex_t mut1
= PTHREAD_MUTEX_INITIALIZER
;
11 static pthread_cond_t cond2
= PTHREAD_COND_INITIALIZER
;
12 static pthread_mutex_t mut2
= PTHREAD_MUTEX_INITIALIZER
;
14 static bool last_round
;
22 pthread_mutex_lock (&mut1
);
29 pthread_cond_signal (&cond2
);
32 pthread_cond_wait (&cond1
, &mut1
);
36 pthread_mutex_unlock (&mut1
);
43 main (int argc
, char *argv
[])
49 bool keeplock
= false;
51 while ((opt
= getopt (argc
, argv
, "n:r:k")) != -1)
55 nthreads
= atol (optarg
);
58 nrounds
= atol (optarg
);
67 pthread_t th
[nthreads
];
69 for (i
= 0; i
< nthreads
; ++i
)
70 if ((err
= pthread_create (&th
[i
], NULL
, cons
, (void *) (long) i
)) != 0)
71 printf ("pthread_create: %s\n", strerror (err
));
73 for (i
= 0; i
< nrounds
; ++i
)
75 pthread_mutex_lock (&mut2
);
77 pthread_cond_wait (&cond2
, &mut2
);
78 pthread_mutex_unlock (&mut2
);
80 pthread_mutex_lock (&mut1
);
82 pthread_mutex_unlock (&mut1
);
89 pthread_cond_broadcast (&cond1
);
92 pthread_mutex_unlock (&mut1
);
95 for (i
= 0; i
< nthreads
; ++i
)
96 if ((err
= pthread_join (th
[i
], NULL
)) != 0)
97 printf ("pthread_create: %s\n", strerror (err
));