了解原子多读写器多写入器寄存器的实现

时间:2016-04-01 13:04:38

标签: java multiprocessing atomic theory multicore

我正在查看原子多读写器多写入器寄存器的以下实现:

 public class AtomicMRMWRegister<T> implements Register<T>{
   private StampedValue<T>[] a_table; // array of atomic MRSW registers
   public AtomicMRMWRegister(int capacity, T init) {
     a_table = (StampedValue<T>[]) new StampedValue[capacity];
     StampedValue<T> value = new StampedValue<T>(init);
     for (int j = 0; j < a_table.length; j++) {
       a_table[j] = value;
     }
    }
   public void write(T value) {
     int me = ThreadID.get();
     StampedValue<T> max = StampedValue.MIN_VALUE;
     for (int i = 0; i < a_table.length; i++) {
       max = StampedValue.max(max, a_table[i]);
     }
     a_table[me] = new StampedValue(max.stamp + 1, value);
   }
   public T read() {
     StampedValue<T> max = StampedValue.MIN_VALUE;
     for (int i = 0; i < a_table.length; i++) {
       max = StampedValue.max(max, a_table[i]);
     }
     return max.value;
   }
 }

public class StampedValue<T> {
  public long stamp;
  public T value;
  // initial value with zero timestamp
  public StampedValue(T init) {
    stamp = 0;
    value = init;
  }
  // later values with timestamp provided
  public StampedValue(long stamp, T value) {
    stamp = stamp;
    value = value;
  }
  public static StampedValue max(StampedValue x, StampedValue y) {
    if (x.stamp > y.stamp) {
      return x;
    } else {
      return y;
    }
  }
  public static StampedValue MIN_VALUE = new StampedValue(null);
}

摘自教科书多处理器编程的艺术source,第86页

当他们声明a_table是原子MRSW寄存器时,他们的意思是什么?当其类型为StampedValue<T>

0 个答案:

没有答案