public void GetAndSet() { AtomicReference <T> ai = new AtomicReference <T>(one); Assert.AreEqual(one, ai.Exchange(zero)); Assert.AreEqual(zero, ai.Exchange(m10)); Assert.AreEqual(m10, ai.Exchange(one)); }
/// <summary> /// Unconditionally sets both the value and mark if any of them are /// not equal. Method <see cref="AreEqual"/> is used to compare the /// value. /// </summary> /// <param name="newValue">the new value /// </param> /// <param name="newMark">the new value for the mark /// </param> public void SetNewAtomicValue(T newValue, bool newMark) { ValueBooleanPair current = Pair; if (!AreEqual(newValue, current._value) || newMark != current._markBit) { _atomicReference.Exchange(new ValueBooleanPair(newValue, newMark)); } }
/// <summary> /// Atomically sets to the given value and returns the previous value. /// </summary> /// <param name="newValue"> /// The new value for the instance. /// </param> /// <returns> /// the previous value of the instance. /// </returns> public T Exchange(T newValue) { return(_atomicReference.Exchange(new ValueHolder <T>(newValue)).Value); }