How can a barrier be implemented with semaphores in Java. Will the following pseudo code work? How can it be written using java Semaphore class.
N
is the number of threads to be waited for at the barrier.
EveryoneHasReachedBarrier
is a conditional variable.
Aquire(mutex)
m = m + 1;
if(m != N)
{
Release(mutex);
Aquire(EveryoneHasReachedBarrier);
}
else
{
m = 0;
Release(mutex);
for(i=0; i<N; i++)
{
Release(EveryoneHasReachedBarrier);
}
}