使用interlocked.CompareExchange将Interlocked.Add添加为Interlocked.add(ref float,ref float)

时间:2013-01-23 08:46:36

标签: multithreading c#-4.0 thread-safety extend interlocked


    public static class InterlockedEx
{
    // AddToTotal safely adds a value to the running total.
    public static float Add(ref float totalValue,float addend)
    {
        float initialValue, computedValue;
        do
        {
            // Save the current running total in a local variable.
            initialValue = totalValue;

            // Add the new value to the running total.
            computedValue = initialValue + addend;

            // CompareExchange compares totalValue to initialValue. If
            // they are not equal, then another thread has updated the
            // running total since this loop started. CompareExchange
            // does not update totalValue. CompareExchange returns the
            // contents of totalValue, which do not equal initialValue,
            // so the loop executes again.
        } while (initialValue != Interlocked.CompareExchange(
            ref totalValue, computedValue, initialValue));
        // If no other thread updated the running total, then 
        // totalValue and initialValue are equal when CompareExchange
        // compares them, and computedValue is stored in totalValue.
        // CompareExchange returns the value that was in totalValue
        // before the update, which is equal to initialValue, so the 
        // loop ends.

        // The function returns computedValue, not totalValue, because
        // totalValue could be changed by another thread between
        // the time the loop ends and the function returns.
        return computedValue;
    }
}

你可以像'Interlocked.Add(ref int32,int32)或Interlocked.Add(ref int64,int64)'那样调用add

我称之为:InterlockedEx.Add(ref subtotalVolume [i],v.Volume);

谁可以提出建议,无论如何都要让它正常运行。

函数的结果与正常函数不相等(不使用TPL)。

0 个答案:

没有答案