. , #. .
public static T InterlockedOperation<T>(ref T location, T operand)
{
T initial, computed;
do
{
initial = location;
computed = op(initial, operand);
}
while (Interlocked.CompareExchange(ref location, computed, initial) != initial);
return computed;
}
InterlockedGreaterThanExchange, .
public static int InterlockedGreaterThanExchange(ref int location, int value)
{
int initial, computed;
do
{
initial = location;
computed = value > initial ? value : initial;
}
while (Interlocked.CompareExchange(ref location, computed, initial) != initial);
return computed;
}