Barrier implementation with mutex and condition variable
- by kkp
I would like to implement a barrier using mutex locks and conditional variables. Please let me know whether my implementation below is fine or not?
static int counter = 0;
static int Gen = 999;
void*
thread_run(void*)
{
pthread_mutex_lock(&lock);
int g = Gen;
if (++counter == nThreads) {
counter = 0;
Gen++;
pthread_cond_broadcast(&cond_var);
} else {
while (Gen == g)
pthread_cond_wait(&cond_var, &lock);
}
pthread_mutex_unlock(&lock);
return NULL;
}