Try a speculative, concurrent, lock free, atomic update until abort condition matches Please let me know if you see any performance improvements, bugs, or anything you'd change and why.

    public static bool TrySpeculativeUpdate(ref int field, out int result,
        Func&lt;int, int&gt; update, Func&lt;int, bool&gt; shouldAbort)
    {
        SpinWait spinWait = new SpinWait();
        while (true)
        {
            int snapshot = field;
            if (shouldAbort(field))
            {
                result = 0;
                return false;
            }
            else
            {
                int calc = update(snapshot);
                if (Interlocked.CompareExchange(ref field, calc, snapshot) == snapshot)
                {
                    result = calc;
                    return true;
                }
            }

            spinWait.SpinOnce();
        }
    }


Can be used like this

    private bool TryIncreaseCapacity(out int newCapacity)
    {
        return TrySpeculativeUpdate(ref _currentCapacity, out newCapacity,
            (currentCapacity) =&gt; currentCapacity + 1,
            (currentCapacity) =&gt; currentCapacity == _maxCapacity);
    }

    if (this.TryIncreaseCapacity(out newCapacity))
    {
        ...
    }
    else
    {
        ...
    }


What I'm really trying to accomplish is the fastest thread safe version of Interlocked.Increment that will stop incrementing at a max value and I will have some way of detecting it stopped incrementing.
I'm not sure I like the method's signature:

public static bool TrySpeculativeUpdate(ref int, out int, Func&lt;int, int&gt;, Func&lt;int, bool&gt;)


It's probably just me, but I like pushing ref and out parameters to the end of the parameters list, so I'd write it like this:

public static bool TrySpeculativeUpdate(Func&lt;int, int&gt;, Func&lt;int, bool&gt;, ref int, out int)


I'd use var to ease reading, and FWIW it might be clearer to pass snapshot instead of field to shouldAbort:

public static bool TrySpeculativeUpdate(Func&lt;int, int&gt; update, Func&lt;int, bool&gt; shouldAbort, ref int field, out int result)
{
    var spinWait = new SpinWait();
    while (true)
    {
        var snapshot = field;

        if (shouldAbort(snapshot))
        {
            result = 0;
            return false;
        }
        else
        {
            var calc = update(snapshot);
            if (Interlocked.CompareExchange(ref field, calc, snapshot) == snapshot)
            {
                result = calc;
                return true;
            }
        }

        spinWait.SpinOnce();
    }
}


That's all I could see. The naming looks about right, but I don't write enough multithreaded code to know for a fact whether this code is optimal or not - it does look good though, I don't see how nesting could be reduced, other than by extacting a tiny little specialized function responsible for this part:

if (Interlocked.CompareExchange(ref field, calc, snapshot) == snapshot)
{
    result = calc;
    return true;
}


But that clearly would be overkill IMO.