view src/main/csharp/jp.ac.u-ryukyu.ie.cr/jungle/transaction/AtomicReference.cs @ 0:dec15de2c6ff

first commit
author Kazuma
date Tue, 21 Jun 2016 17:11:12 +0900
parents
children 02b2ab7bffe6
line wrap: on
line source

using System.Threading;

public class AtomicReference <T> where T : class {
	private T value;
	private bool isSet = false;

	public AtomicReference() { }

	public AtomicReference(T value) {
		this.value = value;
	}

	public T CompareAndSet(T newValue) {
		// change to compere exchange.
		isSet = true;
		return Interlocked.CompareExchange(ref value, value, newValue);
	}

	public bool OptimicSet(T oldvalue) {
		T old;
		do {
			old = value;
		} while (old != CompareAndSet (value));
		return isSet;
	}

	public T Get() {
		return value;
	}
}