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

查看:171
本文介绍了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?

我已经在此处阅读过,并且此处,我正在尝试确保我完全了解两者的内部运作方式.我想了解何时使用一个与另一个相对合适,或者仅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#中,原子性和挥发性之间存在一些轻微的非正交性,因为仅原子类型的字段可以标记为易失性.例如,您可能不会制作易失性加倍.如果说我们将限制读写方式的优化,但仍然允许撕裂",那将是非常奇怪和危险的.由于波动性不会导致原子性,因此您不想让用户认为操作是原子性的,只是因为它也是易失性的.

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-part-part-two/

https://ericlippert .com/2011/06/16/atomicity-volatility-and-mutmutability-are-part-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天全站免登陆