May 25, 2006

System.Threading.Interlocked.CompareExchange

Use
System.Threading.Interlocked.CompareExchange(ref x, y, null);
instead of
if (x == null)
{
    lock (lockObject)
    {
        if (x == null)
        {
            x = y;
        }
    }
}

No comments: