volatile

Does accessing a declared non-volatile object through a volatile reference/pointer confer volatile rules upon said accesses?

萝らか妹 提交于 2019-12-17 16:09:21
问题 This'll be a long one, as to contextualise it and provide as much info as I can, I must meander through various links and quotes - as is often the only way once we enter the C/C++ Standard Rabbit Hole. If you have better citations or any other improvements to this post, please let me know. But to summarise up front, you can blame @zwol for me posting this ;-) and the aim is to find the truth from among two propositions: Do the C and (by import; see comments) C++ Standards require that

volatile vs. mutable in C++

佐手、 提交于 2019-12-17 14:59:57
问题 I have a question about the difference between volatile and mutable. I noticed that both of the two means that it could be changed. What else? Are they the same thing? What's the difference? Where are they applicable? Why the two ideas are proposed? How to use them in different way? Thanks a lot. 回答1: A mutable field can be changed even in an object accessed through a const pointer or reference, or in a const object, so the compiler knows not to stash it in R/O memory. A volatile location is

对java中volatile关键字的理解

和自甴很熟 提交于 2019-12-17 14:34:46
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> volatile关键字的作用 :保证变量的可见性 一定要谨记 volatile 关键字在 Java 代码中仅仅保证这个变量是可见的:它不保证原子性。在那些非原子且可由多个线程访问的易变操作中,一定不能够依赖于 volatile 的同步机制。相反,要使用 java.util.concurrent 包的同步语句、锁类和原子类。它们在设计上能够保证程序是线程安全的。 volatile作用的具体描述 : Java如何保证可见性 Java提供了 volatile 关键字来保证可见性。当使用volatile修饰某个变量时,它会保证对该变量的修改会立即被更新到内存中,并且将其它缓存中对该变量的缓存设置成无效,因此其它线程需要读取该值时必须从主内存中读取,从而得到最新的值。 引自- wangzzu 的博客文章 上面这段话能十分清楚的说明这个变量的用处了吧。下面配合代码更加准确的验证这个问题。 代码引自-- 阿里工程师oldratlee的github Demo类 com.oldratlee.fucking.concurrency.NoPublishDemo 。 Demo说明 主线程中设置属性 stop 为 true ,以控制在 main 启动的任务线程退出。 问题说明 在主线程属性 stop 为 true 后,但任务线程持续运行

关于JVM中long和double的读取原子性

为君一笑 提交于 2019-12-17 14:21:47
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 今天看《Java并发编程实战》的书中,关于long和double的原子性有这么一段话,意思就是在JVM中,对于32位(或者以下)的数值变量都是原子性读写,但是对于long和double这种64位的操作是非原子性。分成两次32位的操作。 在以下这种操作中就会出现读取的数值错误。 线程A先写高位32位操作, 线程B读高位32位, 线程B读地位32位, 线程A写地位32位。 那么解决办法就是对变量加上volatile关键字,volatile关键字有3个主要功能,第一个就是可见性,每次直接从内存读写。第二个就是禁止指令重排序,在JVM中会对指令进行优化,优化的时候可能会对指令进行排序。第三个就是对volatile修饰的变量读取都是原子性的。 但是书上没有说操作系统的位数和JVM的位数。 所以我猜想,只有64位JVM+64位操作系统+64位硬件才能实现对64位long和double的原子性读取。 来源: oschina 链接: https://my.oschina.net/u/2250599/blog/533108

Singleton double-check concurrency issue

我与影子孤独终老i 提交于 2019-12-17 14:03:10
问题 The fallowing clause is taken from jetbrains.net After reading this and some other articles on the web, I still don't understand how is it possible to return null, after the first thread go in to the lock. Some one that does understand it can please help me and explain it in more humanized way? "Consider the following piece of code: public class Foo { private static Foo instance; private static readonly object padlock = new object(); public static Foo Get() { if (instance == null) { lock

并发编程之java内存模型(JMM)

蹲街弑〆低调 提交于 2019-12-17 12:05:22
多线程三大特性 原子性 即一个操作或者多个操作 要么全部执行并且执行的过程不会被任何因素打断,要么就都不执行。 一个很经典的例子就是银行账户转账问题: 比如从账户A向账户B转1000元,那么必然包括2个操作:从账户A减去1000元,往账户B加上1000元。这2个操作必须要具备原子性才能保证不出现一些意外的问题。 我们操作数据也是如此,比如i = i+1;其中就包括,读取i的值,计算i,写入i。这行代码在Java中是不具备原子性的,则多线程运行肯定会出问题,所以也需要我们使用同步和lock这些东西来确保这个特性了。 原子性其实就是保证数据一致、线程安全一部分, 可见性 当多个线程访问同一个变量时,一个线程修改了这个变量的值,其他线程能够立即看得到修改的值。 若两个线程在不同的cpu,那么线程1改变了i的值还没刷新到主存,线程2又使用了i,那么这个i值肯定还是之前的,线程1对变量的修改线程没看到这就是可见性问题。 有序性 程序执行的顺序按照代码的先后顺序执行。 一般来说处理器为了提高程序运行效率,可能会对输入代码进行优化,它不保证程序中各个语句的执行先后顺序同代码中的顺序一致,但是它会保证程序最终执行结果和代码顺序执行的结果是一致的。如下: int a = 10; //语句1 int r = 2; //语句2 a = a + 3; //语句3 r = a*a; //语句4 则因为重排序

volatile的作用

前提是你 提交于 2019-12-17 11:08:41
[转] http://blog.21ic.com/user1/2949/archives/2007/35599.html   一个定义为volatile的变量是说这变量可能会被意想不到地改变,这样,编译器就不会去假设这个变量的值了。精确地说就是,优化器在用到这个变量时必须每次都小心地重新读取这个变量的值,而不是使用保存在寄存器里的备份。下面是volatile变量的几个例子: 1). 并行设备的硬件寄存器(如:状态寄存器) 2). 一个中断服务子程序中会访问到的非自动变量(Non-automatic variables) 3). 多线程应用中被几个任务共享的变量 回答不出这个问题的人是不会被雇佣的。我认为这是区分C程序员和嵌入式系统程序员的最基本的问题。嵌入式系统程序员经常同硬件、中断、RTOS等等打交道,所用这些都要求volatile变量。不懂得volatile内容将会带来灾难。 假设被面试者正确地回答了这是问题(嗯,怀疑这否会是这样),我将稍微深究一下,看一下这家伙是不是直正懂得volatile完全的重要性。 1). 一个参数既可以是const还可以是volatile吗?解释为什么。 2). 一个指针可以是volatile 吗?解释为什么。 3). 下面的函数有什么错误: int square(volatile int *ptr) { return *ptr * *ptr; }

C volatile variables and Cache Memory

人盡茶涼 提交于 2019-12-17 10:19:44
问题 Cache is controlled by cache hardware transparently to processor, so if we use volatile variables in C program, how is it guaranteed that my program reads data each time from the actual memory address specified but not cache. My understanding is that, Volatile keyword tells compiler that the variable references shouldn't be optimized and should be read as programmed in the code. Cache is controlled by cache hardware transparently, hence when processor issues an address, it doesn't know

Is volatile bool for thread control considered wrong?

大城市里の小女人 提交于 2019-12-17 09:20:32
问题 As a result of my answer to this question, I started reading about the keyword volatile and what the consensus is regarding it. I see there is a lot of information about it, some old which seems wrong now and a lot new which says it has almost no place in multi-threaded programming. Hence, I'd like to clarify a specific usage (couldn't find an exact answer here on SO). I also want to point out I do understand the requirements for writing multi-threaded code in general and why volatile is not

Java memory model: volatile variables and happens-before

假装没事ソ 提交于 2019-12-17 09:19:11
问题 I'd like to clarify how happens-before relation works with volatile variables. Let we have the following variables: public static int i, iDst, vDst; public static volatile int v; and thread A: i = 1; v = 2; and thread B: vDst = v; iDst = i; Are the following statements correct in accordance with Java memory model (JMM)? If not, what would be correct interpretation? i = 1 always happens-before v = 2 v = 2 happens-before vDst = v in JMM only if it's actually happens before in time i = 1 happens