简体   繁体   English

C# - 作为关键字真的需要“易变”吗?

[英]C# - Is “volatile” really needed as a keyword?

As I read deeper and deeper into the meaning of the volatile keyword, I keep saying to myself "this is way into implementation , this should not be a part of a high level programming language". 当我更深入地阅读volatile关键字的含义时,我不断对自己说“这是实现的方式 ,这不应该是高级编程语言的一部分”。
I mean, the fact that CPUs cache the data should be interesting for the JIT compiler, not to the C# programmer. 我的意思是,CPU缓存数据的事实对于JIT编译器应该是有趣的,而不是C#程序员。

A considerable alternative might be an attribute (say, VolatileAttribute ). 一个可观的替代方案可能是属性(例如, VolatileAttribute )。

What do you think? 你怎么看?

I think you got side-tracked. 我认为你是侧面跟踪的。 All the tech stuff about caching etc is part of an attempt to explain it in low level terms. 所有关于缓存等技术的东西都是尝试用低级术语解释它的一部分。 The functional description for volatile would be "I might be shared". volatile的功能描述是“我可能会被共享”。 Given that by default nothing can be shared between threads, this is not altogether strange. 鉴于默认情况下线程之间没有任何东西可以共享,这并不奇怪。 And I think fundamental enough to warrant a keyword over an attribute, but I suppose it was largely influenced by historic decisions (C++) 而且我认为基本上足以保证属性上的关键字,但我认为它在很大程度上受到历史决策的影响(C ++)

One way to replace/optimize it is with VolatileRead() and VolatileWrite() calls. 替换/优化它的一种方法是使用VolatileRead()和VolatileWrite()调用。 But that's even more 'implementation'. 但这更像是“实施”。

Well, I certainly agree, it is pretty horrible that such an implementation detail is exposed. 嗯,我当然同意,这样的实现细节暴露是非常可怕的。 It is however the exact same kind of detail that's exposed by the lock keyword. 然而,它与lock关键字所公开的完全相同。 We are still very far removed from that bug generator to be completely removed from our code. 我们仍然远离 bug生成器,从我们的代码中完全删除。

The hardware guys have a lot of work to do. 硬件人员有很多工作要做。 The volatile keyword matters a lot of CPU cores with a weak memory model. volatile关键字对许多具有弱内存模型的CPU内核很重要。 The marketplace isn't been kind to them, the Alpha and the Itanium haven't done well. 市场对他们并不友好,Alpha和Itanium表现不佳。 Not exactly sure why, but I suspect that the difficulty of writing solid threaded code for these cores has a lot to do with it. 不完全确定原因,但我怀疑为这些内核编写实线程代码的难度与它有很大关系。 Getting it wrong is quite a nightmare to debug. 搞错是调试的噩梦。 The verbiage in the MSDN Library documentation for volatile applies to these kind of processors, it otherwise is quite inappropriate for x86/x64 cores and makes it sound that the keyword is doing far more than it really does. 关于volatile的MSDN Library文档中的措辞适用于这些类型的处理器,否则它对于x86 / x64内核来说是非常不合适的,并且使得关键字的效果远远超过实际情况。 Volatile merely prevents variable values from being stored in CPU registers on those cores. Volatile仅防止变量值存储在这些核心上的CPU寄存器中。

Unfortunately, volatile still matters on x86 cores in very select circumstances. 不幸的是,在非常精选的情况下,volatile仍然在x86内核上很重要 I haven't yet found any evidence that it matters on x64 cores. 我还没有找到任何证据表明它在x64核心上很重要。 As far as I can tell, and backed up by the source code in SSCLI20, the Opcodes.Volatile instruction is a no-op for the x64 jitter, changing neither the compiler state nor emitting any machine code. 据我所知,并且SSCLI20中的源代码支持,Opcodes.Volatile指令是x64抖动的无操作,既不改变编译器状态也不改变任何机器代码。 That's heading the right way. 那是正确的方向。

Generic advice is that wherever you're contemplating volatile, using lock or one of the synchronization classes should be your first consideration. 通用建议是,无论您在考虑volatile,使用锁或其中一个同步类都应该是您的首要考虑因素。 Avoiding them to try to optimize your code is a micro-optimization, defeated by the amount of sleep you'll lose when your program is exhibiting thread race problems. 避免他们尝试优化您的代码是微观优化,当您的程序出现线程竞争问题时,您将失去的睡眠数量会失败。

Using an attribute would be acceptable, if it were the other way around, that is, the compiler would assume that all varaibles are volatile, unless explicitly marked with an attribute saying it was safe. 使用属性是可以接受的,如果是相反的方式,也就是说,编译器会假设所有变量都是易失性的,除非明确标记了一个属性,说它是安全的。 That would be incredibly determental to performance. 这对于表现来说是非常不利的。

Hence it's assumed that, since having a variable's value changed outside of the view of the compiler is an abberation, the compiler would assume that it is not happeing. 因此,假设由于在编译器视图之外更改了变量的值是一种异常,编译器会认为它没有发生。

However, That could happen in a program so the language itself must have a way of showing that. 但是,这可能发生在一个程序中,因此语言本身必须有一种方式来表明这一点。

Also, you seems confused about "implementation details". 此外,您似乎对“实施细节”感到困惑。 The term refers to things the compiler does behind your back. 该术语指的是编译器背后的事情。 This is not the case here. 这不是这种情况。 Your code is modifying a varaible outside of the view of the compiler. 您的代码正在修改编译器视图之外的变量。 SInce it's in your code, it will always be true. 它是在你的代码中,它将永远是真的。 Hence the langauge must be able to indicate that. 因此,语言必须能够表明这一点。

IIIRC, in C++ volatile was mainly about memory mapped I/O rather than cache. IIIRC,在C ++中volatile主要是关于内存映射的I / O而不是缓存。 If you read the same port twice you get different answers. 如果您两次阅读相同的端口,您会得到不同的答案。 Still, I would agree with your assessment that this is more cleanly expressed in C# as an attribute. 不过,我同意你的评估,即在C#中作为属性更清晰地表达。

On the other hand, most actual uses of volatile in C# can better be understood as thread lock anyway, so the choice of volatile is may be a little unfortunate. 另一方面,C#中volatile的大多数实际用途无论如何都可以更好地理解为线程锁定,因此volatile的选择可能有点不幸。

Edit : Just to add: two links to show that in C/C++ `volatile is explicitly not for multithreading. 编辑 :只是添加:两个链接显示在C / C ++中`volatile 明确 不是用于多线程。

volatile in c# emits the correct barriers, or fences, which would matter to the programmer doing multi-thread work. c#中的volatile会发出正确的障碍或栅栏,这对程序员进行多线程工作很重要。 Bear in mind, that the compiler, runtime, and the processor all can reorder reads/writes to some degree (each has its own rules). 请记住,编译器,运行时和处理器都可以在某种程度上重新排序读/写(每个都有自己的规则)。 Though CLR 2.0 has a stronger memory model that what CLI ECMA specifies, the CLR memory model still is not the strictest memory model so you have a need for volatile in C#. 虽然CLR 2.0具有比EC ECMA指定的更强的内存模型,但CLR内存模型仍然不是最严格的内存模型,因此您需要在C#中使用volatile

As an attribute, I don't think you can use attributes inside a method body, so the keyword is necessary. 作为属性,我认为您不能在方法体内使用属性,因此关键字是必需的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM