Reputation: 596
anyone has any idea how is Barrier Synchronization
implemented internally in scala? I am guessing it is using Semaphores
, but I am wondering how exactly it works, does anyone know?
I guess one naive way to think of this might be to create an array of Semaphores one for each process. Any better / more efficient implementations?
Upvotes: 2
Views: 2754
Reputation: 297265
I'd use either java.util.concurrent.CountDownLatch
or java.util.concurrent.CyclicBarrier
.
Upvotes: 4
Reputation: 596
Ok, sorry for this guys, I have found it after all. here it is in case anyone is interested.
class Barrier(n:Int){
assert(n>1);
private var waiting = 0; // number of processes currently waiting
private val waitSem = new Semaphore; waitSem.down
private val mutex = new Semaphore;
def sync = {
mutex.down;
if(waiting==n-1){ waitSem.up; }
else{
waiting+=1; mutex.up; waitSem.down;
// Wait until woken
waiting-=1;
if(waiting==0) mutex.up; else waitSem.up;
}
}
}
Upvotes: 1