C# bool 是原子的,为什么 volatile 有效 [英] C# bool is atomic, why is volatile valid

查看:34
本文介绍了C# bool 是原子的,为什么 volatile 有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C# 中,我们知道 bool 是原子的 - 那么为什么将其标记为 volatile 是有效的?两者有什么区别,什么是好的(甚至实用的)用例?

In C#, we know that a bool is atomic - then why is it valid to mark it as volatile? what is the difference and what is a good (or even practical) use-case for one versus the other?

bool _isPending;

对比

volatile bool _isPending; // Is this realistic, or insanity?

我已经阅读了 here这里,我正在努力确保我完全理解两者的内部运作.我想了解何时使用一个与另一个比较合适,或者仅 bool 是否足够.

I have done some reading here and here, and I'm trying to ensure that I fully understand the inner workings of the two. I want to understand when it is appropriate to use one vs the other, or if just bool is enough.

推荐答案

在 C# 中,我们知道 bool 是原子的 - 那么为什么将它标记为 volatile 是有效的呢?两者有什么区别,什么是好的(甚至实用的)用例?

In C#, we know that a bool is atomic - then why is it valid to mark it as volatile? what is the difference and what is a good (or even practical) use-case for one versus the other?

您的问题的假设是您认为 volatile 使访问原子化.但是波动性和原子性是完全不同的东西,所以不要把它们混为一谈.

The supposition of your question is that you believe that volatile makes an access atomic. But volatility and atomicity are completely different things, so stop conflating them.

波动性是一种属性,编译器和运行时被限制进行某些优化,包括在时间上相对于彼此向前和向后移动变量的读取和写入,更一般地,关于其他重要事件,例如启动和停止线程、运行构造函数等.请参阅 C# 规范,了解有关可见副作用如何重新排序操作的详细列表.

Volatility is the property that the compiler and runtime are restricted from making certain optimizations involving moving reads and writes of variables forwards and backwards in time with respect to each other, and more generally, with respect to other important events such as starting and stopping threads, running constructors, and so on. Consult the C# specification for a detailed list of how operations may or may not be re-ordered with respect to visible side effects.

原子性是特定操作只能被观察为未开始或完全完成的属性,并且永远不会半途而废".

从定义中可以看出,这两件事彼此之间没有任何关系任何.

As you can see from the definitions, those two things have nothing whatsoever to do with each other.

在 C# 中,所有对大小为 4 或更小的引用、布尔值和整数类型的访问都保证是原子的.

In C#, all accesses to references, bools, and integer types of size 4 and smaller are guaranteed to be atomic.

现在,在 C# 中,原子性和易失性之间存在一些轻微的非正交性,因为只有原子类型的字段可能被标记为易失性.例如,您不能制作 volatile double.说我们将限制如何优化读写但仍然允许撕裂"真的很奇怪和危险.由于波动性不会导致原子性,因此您不想让用户仅仅因为它也是可变的,就认为操作是原子的.

Now, in C# there is some slight non-orthogonality between atomicity and volatility, in that only fields of atomic types may be marked as volatile. You may not make a volatile double, for example. It would be really weird and dangerous to say "we're going to restrict how reads and writes may be optimized but still allow tearing". Since volatility does not cause atomicity, you don't want to put users in a position of thinking that an operation is atomic just because it is also volatile.

您应该阅读我的系列文章,这些文章更详细地解释了这些事物之间的区别,以及 volatile 的实际作用,以及为什么您对安全使用它的理解还不够.

You should read my series of articles that explains in far more detail what the differences between these things are, and what volatile actually does, and why you do not understand nearly enough to be using it safely.

https://ericlippert.com/2011/05/26/atomicity-volatility-and-immutability-are-different-part-one/

https://ericlippert.com/2011/05/31/atomicity-volatility-and-immutability-are-different-part-two/

https://ericlippert.com/2011/06/16/atomicity-volatility-and-immutability-are-different-part-three/

https://web.archive.org/web/20160323025740/http://blog.coverity.com/2014/03/12/can-skip-lock-reading-integer/

如果您在阅读完所有内容后认为您了解波动性,我邀请您尝试解决我在这里提出的难题:

If you think you understand volatility after reading all that, I invite you to try to solve the puzzle I pose here:

https://web.archive.org/web/20160729162225/http://blog.coverity.com/2014/03/26/reordering-optimizations/

这篇关于C# bool 是原子的,为什么 volatile 有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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