Skip to content

volatile

csharp
private static volatile bool _complete;

private static void Main()
{
    var t = new Thread(() =>
    {
        var toggle = false;
        while (!_complete)
        {
            toggle = !toggle;
        }
    });
    t.Start();
    Thread.Sleep(1000); //让其他线程起来
    _complete = true;
    t.Join(); // 不使用volatile则会无线阻止
}
private static volatile bool _complete;

private static void Main()
{
    var t = new Thread(() =>
    {
        var toggle = false;
        while (!_complete)
        {
            toggle = !toggle;
        }
    });
    t.Start();
    Thread.Sleep(1000); //让其他线程起来
    _complete = true;
    t.Join(); // 不使用volatile则会无线阻止
}

Release 下编译

reference

Volatile keyword in C# – memory model explained

Memory Barriers and Volatility