barrier code and waiting for all thread to reach rendezvous and then enter critical section

Viewed 133
semaphore mutex = 1;
semaphore barrier = 0;
int count = 0;
void barrier-done() {
    wait(mutex);
    count++;
    if (count < N ) {
      post(mutex);
      wait(barrier);
    }
    else {
      post(mutex);
      count = 0;
      for (int i = 1; i < N; i++) {
      post(barrier);
    }
  }
}

does anyone know the problem with this code? I'm trying to implement a code for barrier.

2 Answers

Assuming N is the number of threads you are expecting to wait for the barrier.

For Example N=10, then the threads 1 to 9 will have if condition true and they will wait for barrier. The 10th Thread calling this will have that condition false because (10 !< 10). So it will go ahead and post barrier 9 times.

I am not sure of the exact situation you want to achieve. But, this is what I understood from your code. May be you might need to tweak the if condition a bit.

I had the same issue but the problem is that you can't use minus sign in the name of function "barrier-done" after fixing this bug the code will be correct.

Related