volatile

Why readonly and volatile modifiers are mutually exclusive?

喜欢而已 提交于 2020-01-01 08:56:00
问题 I have a reference-type variable that is readonly , because the reference never change, only its properties. When I tried to add the volatile modifier to it the compiled warned me that it wouldn't let both modifiers apply to the same variable. But I think I need it to be volatile because I don't want to have caching problems when reading its properties. Am I missing anything? Or is the compiler wrong? Update As Martin stated in one of the comments below: Both readonly and volatile modifiers

volatile关键字详解

烂漫一生 提交于 2020-01-01 05:00:25
我们经常这样写单例模式: public class LzySingleInstance { public static volatile LzySingleInstance lzySingleInstance; private LzySingleInstance(){ } public static LzySingleInstance getLzySingleInstance(){ if (lzySingleInstance == null){ synchronized (LzySingleInstance.class){ if (lzySingleInstance == null){ lzySingleInstance = new LzySingleInstance(); } } } return lzySingleInstance; } } 那么,有没有思考过一个问题:volatile到底是什么作用? volatile的特性: 1.线程可见性; 一个线程操作了对象,其他线程立马可见。 2.禁止重排序; 保证对象被实例化完成! 来源: CSDN 作者: AD钙奶-lalala 链接: https://blog.csdn.net/qq_36428821/article/details/103765424

May accesses to volatiles be reordered?

安稳与你 提交于 2019-12-31 17:13:32
问题 Consider the following sequence of writes to volatile memory, which I've taken from David Chisnall's article at InformIT, "Understanding C11 and C++11 Atomics": volatile int a = 1; volatile int b = 2; a = 3; My understanding from C++98 was that these operations could not be reordered, per C++98 1.9: conforming implementations are required to emulate (only) the observable behavior of the abstract machine as explained below ... The observable behavior of the abstract machine is its sequence of

Freeing (vfree-ing) pointer to volatile data

若如初见. 提交于 2019-12-31 04:06:26
问题 volatile seems to be a never ending question of every one. I thought I knew everything about it, but then I encountered this: So, I have a piece of memory shared between threads and I defined it like this: volatile type *name; If it makes you feel better, you can imagine type is just an int . This means I have a pointer (that is not volatile) to some data that are volatile. So, for example when it comes to optimizing, the compiler can cache the value of name but not name[0] . Am I right? So,

C++ volatile required when spinning on boost::shared_ptr operator bool()? [duplicate]

放肆的年华 提交于 2019-12-30 12:23:59
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: When to use volatile with multi threading? I have two threads referencing the same boost::shared_ptr : boost::shared_ptr<Widget> shared; On thread is spinning, waiting for the other thread to reset the boost::shared_ptr : while(shared) boost::thread::yield(); And at some point the other thread will call: shared.reset(); My question is whether or not I need to declare the shared pointer as volatile to prevent the

C++ volatile required when spinning on boost::shared_ptr operator bool()? [duplicate]

左心房为你撑大大i 提交于 2019-12-30 12:23:11
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: When to use volatile with multi threading? I have two threads referencing the same boost::shared_ptr : boost::shared_ptr<Widget> shared; On thread is spinning, waiting for the other thread to reset the boost::shared_ptr : while(shared) boost::thread::yield(); And at some point the other thread will call: shared.reset(); My question is whether or not I need to declare the shared pointer as volatile to prevent the

Does making reference variable volatile, make all its field volatile too in java?

♀尐吖头ヾ 提交于 2019-12-30 11:21:19
问题 I have read that making reference variable volatile, does not make its inner fields volatile.But i tried with below example where it looks like volatile nature is applied to inner fields of class as well. User.java:- // user class having field "flag" set as true. public class User { private boolean flag=true; public boolean isFlag() { return flag; } public void setFlag(boolean flag) { this.flag = flag; } } MyRunnableThread1.java:- here i have made "user" as volatile and not its inner field

volatile overloading?

五迷三道 提交于 2019-12-30 01:12:21
问题 I heard that volatile is factor of overloading like const. If a function is overloaded by volatile parameter, when is the volatile-version called? I can't imagine a situation when the volatile-version is called. 回答1: Volatile can be applied to parameters, but it is not a factor of overloading when applied directly to the parameter. It is however possible to use it to distinguish types of the parameter. For example, this is legal: void f(int &p) {}; //reference to int void f(volatile int &p) {

Java面试官最爱问的volatile关键字

血红的双手。 提交于 2019-12-29 23:11:16
在Java的面试当中,面试官最爱问的就是volatile关键字相关的问题。经过多次面试之后,你是否思考过,为什么他们那么爱问volatile关键字相关的问题?而对于你,如果作为面试官,是否也会考虑采用volatile关键字作为切入点呢? 为什么爱问volatile关键字 爱问volatile关键字的面试官,大多数情况下都是有一定功底的,因为volatile作为切入点,往底层走可以切入Java内存模型(JMM),往并发方向走又可接切入Java并发编程,当然,再深入追究,JVM的底层操作、字节码的操作、单例都可以牵扯出来。 所以说懂的人提问题都是有门道的。那么,先整体来看看volatile关键字都设计到哪些点:内存可见性(JMM特性)、原子性(JMM特性)、禁止指令重排、线程并发、与synchronized的区别……再往深层次挖,可能就涉及到字节码、JVM等。 不过值得庆幸的是,如果你已经学习了微信公众号“程序新视界”JVM系列的文章,上面的知识点已经不是什么问题了,权当是复习了。那么,下面就以面试官提问的形式,在不看答案的情况下,尝试回答,看看学习效果如何。夺命连环问,开始…… 面试官:说说volatile关键字的特性 被volatile修饰的共享变量,就具有了以下两点特性: 保证了不同线程对该变量操作的内存可见性; 禁止指令重排序; 回答的很好,点出了volatile关键字两大特性

对volatile不具有原子性的理解

随声附和 提交于 2019-12-29 20:34:56
在阅读多线程书籍的时候,对volatile的原子性产生了疑问,问题类似于这篇 文章 所阐述的那样。经过一番思考给出自己的理解。 我们知道对于可见性,Java提供了volatile关键字来保证 可见性 、 有序性 。 但不保证原子性 。 普通的共享变量不能保证可见性,因为普通共享变量被修改之后,什么时候被写入主存是不确定的,当其他线程去读取时,此时内存中可能还是原来的旧值,因此无法保证可见性。   背景:为了提高处理速度,处理器不直接和内存进行通信,而是先将系统内存的数据读到内部缓存(L1,L2或其他)后再进行操作,但操作完不知道何时会写到内存。 如果对声明了volatile的变量进行写操作 ,JVM就会向处理器发送一条指令,将这个变量所在缓存行的数据写回到系统内存。但是,就算写回到内存,如果其他处理器缓存的值还是旧的,再执行计算操作就会有问题。 在多处理器下,为了保证各个处理器的缓存是一致的,就会 实现缓存一致性协议 ,每个处理器通过嗅探在总线上传播的数据来检查自己缓存的值是不是过期了,当处理器发现自己缓存行对应的内存地址被修改,就会将当前处理器的缓存行设置成无效状态,当处理器对这个数据进行修改操作的时候,会重新从系统内存中把数据读到处理器缓存里。 总结下来 : 第一:使用volatile关键字会强制将修改的值立即写入主存; 第二:使用volatile关键字的话,当线程2进行修改时