using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace CommonLang { public class State { private T mValue; public T Value { get { return mValue; } } public State(T value) { this.mValue = value; } public bool ChangeState(T value) { if (!mValue.Equals(value)) { mValue = value; return true; } return false; } public bool CompareChangeState(T expect, T new_value) { if (expect.Equals(mValue)) { mValue = new_value; return true; } return false; } } public class AtomicState { private T mValue; public T Value { get { lock (this) { return mValue; } } } public AtomicState(T value) { this.mValue = value; } public bool ChangeState(T value) { lock (this) { if (!mValue.Equals(value)) { mValue = value; return true; } return false; } } public bool CompareChangeState(T expect, T new_value) { lock (this) { if (expect.Equals(mValue)) { mValue = new_value; return true; } return false; } } } }