关于挥发性用法的此规则是否严格? [英] Is this rule about volatile usage strict?

查看:79
本文介绍了关于挥发性用法的此规则是否严格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过这句话:

一般规则是,如果您有 必须必须的原始类型的变量 在多个线程之间共享, 声明那些变量 易失性

the general rule is, if you have variables of primitive type that must be shared among multiple threads, declare those variables volatile

来自本文,和这句话:

通常,任何可能是 异步未日期的应为 声明是易失的.

In general, any data that may be undated asynchronously should be declared to be volatile.

来自此页面,现在考虑了我想知道的这个引入的规则,您能否提出一个例子,尽管存在对数据的异步访问,但声明数据易失性在实践中没有用,或者没有这样的例外情况,并且规则很严格.

from this page, now considering this introduced rule I'd like to know could you bring an example of a case where despite existence of asynchronous access to a data declaring that data volatile has no use in practice or there's no such exceptional case and the rule is strict.

推荐答案

我记得该文章发表的时间,也记得后来关于comp.lang.c ++.moderated的无休止的讨论.

I remember when that article was published and I remember the endless discussions that then followed on comp.lang.c++.moderated.

IIRC, Andrei劫持了volatile关键字 ,以使用它来区分不同的函数重载. (请参见 Scott Meyers的这篇文章 它使编译器在您搞砸了的情况下可以抓住您 对对象的受保护和不受保护的访问(非常就像编译器捕获的一样,您应该尝试修改常量).但是,除了它可以为您提供帮助之外, 它与实际保护对对象的并发访问没有任何关系 .

IIRC, Andrei hijacks the volatile keyword to use it to discriminate between different function overloads. (See this article by Scott Meyers for another such an idea.) What he does is brilliant, in that it allows the compiler to catch you if you mess up protected and unprotected access to objects (very much like the compiler catches you should you try to modify a constant). But besides the fact that it helps you, it has nothing to do with actually protecting concurrent access to objects.

问题是,只有90%的人对这篇文章一目了然,而 他们看到的只是同一篇文章中的volatile和线程" .然后,根据他们的知识,他们 得出错误的结论,认为volatile对线程有益 (您似乎已经这样做了),或者他们因为领导他人而对他大喊大叫得出错误的结论.
似乎很少有人能够真正地阅读该文章并理解他的实际所作所为.

The problem is only that 90% of the people have one glance at the article and all they see is volatile and "threads" in the same article. Depending on their knowledge, they then either draw the wrong conclusion that volatile is good for threads (you seem to have done so) or they yell at him for leading others to draw the wrong conclusions.
Very few people seem to be able to actually read the article thoroughly and understand what he really does.

这篇关于关于挥发性用法的此规则是否严格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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