cs442_barrier_solution
class Barrier
{
	private int cIndex = 0;
	private CounterState state = null;
	public Barrier(CounterState state) 
	{
		this.state = state;
	}
	
	public void barrier (int n)
	{
		int val = state.readCounter(cIndex);
		if (val == n-1) 
			state.writeCounter(1-cIndex, 0);
		val = state.incAndReadCounter(cIndex);
		while (val != n) {
			try { Thread.sleep(1); } catch(Exception e) {}
			val = state.readCounter(cIndex);
		}
		cIndex = 1 - cIndex;
	}
}
cs442_barrier_solution.txt · Last modified: 2012/10/19 17:55 by bgedik

Page Tools