Skip to main content
Journey Uncommon Logo
JourneyUncommon
hardC#.NET memory modelSingle-choice MCQ

Two threads run this with `_flag` and `_value` as plain (non-volatile) `int` fields. Under the ECMA-335 memory model (ignore the stronger x86 implementation), why is it legal for the consumer to print 0?

// shared: int _value = 0; bool _flag = false; // Producer thread _value = 42; _flag = true; // Consumer thread if (_flag) Console.WriteLine(_value); // can this print 0?