C#编译器的优化和volatile关键字 [英] C# compiler optimization and volatile keyword

查看:189
本文介绍了C#编译器的优化和volatile关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过一些帖子挥发性关键字和行为不包含此关键字。



我特别测试从答案的代码说明volatile关键字的使用在C# 。在运行时,我观察到发布模式除外行为,没有附加调试器。到这一点,是没有问题的。



所以,据我了解,下面的代码应该永远不会退出。

 公共类节目
{
私人布尔stopThread;

公共无效测试()
{
,而(!stopThread){} //读取stopThread未标记为挥发性
Console.WriteLine(停止。 );
}


私有静态无效的主要()
{
计划项目=新计划();

线程线程=新主题(program.Test);
thread.Start();

Console.WriteLine(按任意键停止线程。);
Console.ReadKey();

Console.WriteLine(等待线程。);
program.stopThread = TRUE;

的Thread.join(); //等待线程停止。
}
}



为什么会退出?即使在发行方式,无需调试?



更新



从的 volatile关键字的使用http://stackoverflow.com/questions/133270/illustrating-usage-of-the-volatile-keyword-in-c-sharp\">Illustrating

 私人布尔退出; 

公共无效测试()
{
的Thread.Sleep(500);
出口= TRUE;
Console.WriteLine(退出请求);
}

私有静态无效的主要()
{
计划项目=新计划();

//启动线程
线程线程=新主题(program.Test);
thread.Start();

Console.WriteLine(等待线程。);
,而(!program.exit){}
}

这程序会后不会退出在Release模式,不附加调试器。


解决方案

所以,就我了解,下面应该永远不会退出。




没有,它的可以的停止。它只是没有的保证



它不会停止,我目前正在运行的机器,例如 - 但同样我可以尝试在另一台机器上完全相同的可执行文件,它可能会表现得很好。这将取决于由它运行在CLR所用的确切存储器模型的语义。这将由底层架构的影响和所用甚至有可能确切的CPU。



要注意,这不是很重要的 C#编译器的这决定了做什么用挥发性字段 - C#编译器只是表示使用的 System.Runtime.CompilerServices.IsVolatile 。随后的 JIT 的可以工作了什么,在遵守相关合同条款的意思。


I have read some posts about volatile keyword and behaviour without this keyword.

I've especially tested the code from the answer to Illustrating usage of the volatile keyword in C#. When running, I observe the excepted behaviour in Release mode, without debugger attached. Up to that point, there is no problem.

So, as far as I understand, the following code should never exit.

public class Program
{
    private bool stopThread;

    public void Test()
    {
        while (!stopThread) { }  // Read stopThread which is not marked as volatile
        Console.WriteLine("Stopped.");
    }


    private static void Main()
    {
        Program program = new Program();

        Thread thread = new Thread(program.Test);
        thread.Start();

        Console.WriteLine("Press a key to stop the thread.");
        Console.ReadKey();

        Console.WriteLine("Waiting for thread.");
        program.stopThread = true;

        thread.Join();  // Waits for the thread to stop.
    }
}

Why does it exit? Even in Release mode, without debugger?

Update

An adaptation of the code from Illustrating usage of the volatile keyword in C#.

private bool exit;

public void Test()
{
    Thread.Sleep(500);
    exit = true;
    Console.WriteLine("Exit requested.");
}

private static void Main()
{
    Program program = new Program();

    // Starts the thread
    Thread thread = new Thread(program.Test);
    thread.Start();

    Console.WriteLine("Waiting for thread.");
    while (!program.exit) { }
}

This program does not exit after in Release mode, without debugger attached.

解决方案

So, as far as I understand, the following should never exit.

No, it can stop. It just isn't guaranteed to.

It doesn't stop on the machine I'm currently running on, for example - but equally I could try the exact same executable on another machine and it might behave fine. It will depend on the exact memory model semantics used by the CLR it runs on. That will be affected by the underlying architecture and potentially even the exact CPU being used.

It's important to note that it's not the C# compiler which determines what to do with a volatile field - the C# compiler just indicates the volatility in the metadata using System.Runtime.CompilerServices.IsVolatile. Then the JIT can work out what that means in terms of obeying the relevant contracts.

这篇关于C#编译器的优化和volatile关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆