0
public class cyclicBarrier {
    private static int n;
    private static int count;
    private static semaphore mutex;
    private static semaphore turnstile;
    private static semaphore turnstile2;

    public cyclicBarrier(int n){
        this.n = n;
        this.count = 0;
        this.mutex = new semaphore(1);
        this.turnstile = new semaphore(0);
        this.turnstile2 = new semaphore(0);
    }

    public synchronized void down() throws InterruptedException{
        this.phase1();
        this.phase2();
    }

    private synchronized void phase1() throws InterruptedException {
        this.mutex.down();
        this.count++;
        if (this.count == this.n){
            for (int i=0; i< this.n; i++){
                          this.turnstile.signal();
                    }
        }
        this.mutex.signal();
        this.turnstile.down();
    }

    private synchronized void phase2() throws InterruptedException {
        this.mutex.down();
        this.count--;
        if (this.count == 0){
            for (int i=0; i< this.n; i++){
                          this.turnstile2.signal();
                    }
        }
        this.mutex.signal();
        this.turnstile2.down();
    }
}

&& here is class semaphore just in case

public class semaphore{
    private int counter;

    public semaphore(int number){
        if (number > 0) {
            this.counter = number;
        }
    }

    public synchronized void signal(){
        this.counter++;
        notifyAll();
    }

    public synchronized void down() throws InterruptedException{
        while (this.counter <= 0){
            wait();
        }
        this.counter--;
    }
}

This is a code that I wrote to implement Cyclicbarriers using threads. I took the pseudo code from the book along with the notes on deadlocks so I think its pretty much ok "there might be a bug though". The first phase is for "arriving threads" and the 2nd phase is for "running threads in the critical region together". My question is as follows... how to change the code in order to consider specific types of threads? For instance, there are hydrogen threads and oxygen threads and I want them to execute bond() everytime there are 2 hydrogen atoms and 1 oxygen atom at the barrier. Thank you in advance.

4

2 回答 2

1

Instead of giving you an answer, I'll give you a hint. Let me know if you want me the full answer.

First, fix your code according to my answer to your other question—if I'm right about its flaws, that is :)

Second, imagine that we have m threads and a barrier(n) where m >= 2n, each thread doing

while True:
    noncritical()
    barrier.enter() # e.g. phase1()
    critical()
    barrier.leave() # e.g. phase2()

If you implement enter() and leave() as I have suggested, you may have 2n threads in the critical section. To fix this problem, I have come up with the following construct:

class N_At_A_Time_Barrier(n):
    fields:
        occupied = Semaphore(n)
        barrier = Barrier(n)
    method enter():
        occupied.down()
        barrier.down()
    method leave():
        occupied.up()

In the terminology of The Little book of Semaphores, occupied is a Multiplex. The hint: the solution to the H2O problem is very similar to this. My solution uses a Barrier(1+2) and... something else :)

(Footnote: there are more efficient ways of implementing an N_At_A_Time_Barrier, where each group of n threads has their own shared state set up by the first thread to arrive, but that's a lot more complicated and I don't think it relates to the H2O problem as neatly.)

于 2014-11-11T11:55:22.210 回答
0

do you need to implement Cyclicbarriers yourself? (homework) or can you use the JDK's implementation? http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CyclicBarrier.html

Maybe you could extend the await() method, to include your Atom kind, and add a method that tells you what atoms are waiting at the barrier, if you find the condition of having say H20, you can call a callback method, in this case bond(Atoms[] atoms)

于 2013-03-31T01:59:12.103 回答