Does Interlocked guarantee visibility to other threads in C# or do I still have to use volatile?

Posted by Lirik on Stack Overflow See other posts from Stack Overflow or by Lirik
Published on 2010-03-19T05:48:44Z Indexed on 2010/03/19 5:51 UTC
Read the original article Hit count: 195

I've been reading the answer to a similar question, but I'm still a little confused... Abel had a great answer, but this is the part that I'm unsure about:

...declaring a variable volatile makes it volatile for every single access. It is impossible to force this behavior any other way, hence volatile cannot be replaced with Interlocked. This is needed in scenarios where other libraries, interfaces or hardware can access your variable and update it anytime, or need the most recent version.

Does Interlocked guarantee visibility of the atomic operation to all threads, or do I still have to use the volatile keyword on the value in order to guarantee visibility of the change?

Here is my example:

public class CountDownLatch 
{
    private volatile int m_remain; // <--- do I need the volatile keyword there since I'm using Interlocked?
    private EventWaitHandle m_event;

    public CountDownLatch (int count)
    {
        Reset(count);
    }

    public void Reset(int count)
    {
        if (count < 0)
            throw new ArgumentOutOfRangeException();
        m_remain = count;
        m_event = new ManualResetEvent(false);
        if (m_remain == 0)
        {
            m_event.Set();
        }
    }

    public void Signal()
    {
        // The last thread to signal also sets the event.
        if (Interlocked.Decrement(ref m_remain) == 0)
            m_event.Set();
    }

    public void Wait()
    {
        m_event.WaitOne();
    }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about multithreading