123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace CommonLang
- {
- public class State<T>
- {
- 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<T>
- {
- 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;
- }
- }
- }
- }
|