Does Interlocked guarantee visibility to other threads in C# or do I still have to use volatile?
- by Lirik
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();
}
}