I changed the program specified in Non-Blocking Synchronization as follows:
class DemoProg
{
int _answer;
bool _complete;
public void StartDemo()
{
Thread t1 = new Thread(A);
Thread t2 = new Thread(B);
t1.Start();
t2.Start();
}
void A()
{
for (int i = 0; i < 1000000; i++)
_answer = 123;
Thread.MemoryBarrier();
_complete = true;
Thread.MemoryBarrier();
Console.WriteLine("Exiting A");
}
void B()
{
Thread.MemoryBarrier();
if (_complete)
{
Thread.MemoryBarrier();
Console.WriteLine(_answer);
}
Console.WriteLine("Exiting B");
}
}
The article says that they ensure that if B ran after A, reading _complete would evaluate to true.→ they mean memory barriers.
Even if I remove the memory barriers, no changes in the output occur. , and this does not guarantee that if the condition is true.
Did I interpret this incorrectly?
Thank.
source
share