I've read many articles that compare volatiles and locks. From my understanding lock
guarantees that only one thread can use the lock and run the critical code in parallel, and volatile
disables the memory optimization - so they are not replaceable. Using volatile
should not replace a lock
. Although most of the articles are not using lock
when they show examples of volatile
.
Say I have such a super simple class:
class Foo
{
private bool bar;
private static readonly object barLock = new object();
public void ToggleBar()
{
lock (barLock)
{
bar = !bar;
}
}
public bool ReadBar()
{
lock (barLock)
{
return bar;
}
}
}
Say the ToggleBar
is used by thread A and ReadBar
by thread B, only.
The lock
protects from the two threads to access the variable in parallel. But should bar
be volatile
as well?
In most of the articles I've read, this was the "recommended" solution (also Interlocked class), but for my understanding it, it doesn't solve the issue that volatile
solves - even with the lock thread B may read an old value of bar
due to memory optimizations and caching, isn't it?
Shouldn't bar
be a volatile
in addition to the lock
in this case?