CSharp - Using SpinLocks

From PeformIQ Upgrade
Jump to navigation Jump to search

Example

From - http://stackoverflow.com/questions/15164408/lock-free-concurrent-queue

This code snippet is from ConcurrentQueue implementation.

internal bool TryPeek(out T result) 
{
    result = default(T); 
    int lowLocal = Low;
    if (lowLocal > High)
        return false;
    SpinWait spin = new SpinWait(); 
    while (m_state[lowLocal] == 0)
    { 
        spin.SpinOnce(); 
    }
    result = m_array[lowLocal]; 
    return true;
}

Also see - http://msdn.microsoft.com/en-us/library/hh228603.aspx