Here is a generic pattern to simulate a lock operation.
public static T InterlockedOperation<T>(ref T location, T value)
{
T initial, computed;
do
{
initial = location;
computed = op(initial, value);
}
while (Interlocked.CompareExchange(ref location, computed, initial) != initial);
return computed;
}
min - . , . , . , . volatile Thread.MemoryBarrier .
: , min a. , , computed = initial | value do computed = initial < value ? initial : value. .