using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace WinDll.Utils { /// /// A class to control access to a particulair value, Threadsafe /// public class LockedValue : IDisposable { private T value; private Semaphore sem; /// /// Create a new LockedValue and allow only 1 thing to access it at a time /// /// The value to monitor public LockedValue(T value):this(value, 1){ } /// /// Create a new LockedValue and allow @max@ things to access it at a time /// /// The value to monitor /// public LockedValue(T value, int max) { this.value = value; this.sem = new Semaphore(max, 10, "LockedValue" + this.GetHashCode()); } /// /// Retreive the LockedValue /// /// LockedValue public T pull() { this.sem.WaitOne(); return this.value; } /// /// Release the LockedValue /// public void pulse() { this.sem.Release(); } /// /// Unsafe retreiving of the value. This should not be used under normal conditions! /// /// LockedValue public T unsafePull() { return this.value; } public void Dispose() { this.sem.Close(); } } }