volatile

Example of a memory consistency error when using volatile keyword?

余生颓废 提交于 2019-12-04 22:13:10
问题 From docs: Using volatile variables reduces the risk of memory consistency errors But this means that sometimes volatile variables don't work correct? Strange how it can be used - for my opinion it is very bad code that sometimes work sometimes not. I tried to Google but didn't find example memory consistency error with volatile. Could you please propose one? 回答1: The issue is not so much that volatile works unreliably. It always works the way it is supposed to work. The problem is that the

Volatile的应用场景

吃可爱长大的小学妹 提交于 2019-12-04 22:07:57
1、当一个变量可能会被意想不到的更新时,要使用volatile来声明该变量,告诉编译器它所修饰的变量的值可能会在任何时刻被意外的更新。 2、语法   volatile int foo;   int volatile foo;      volatile int * foo; int volatile * foo;      int * volatile foo;   int volatile * volatile foo; 3、场景    1)并行设备的硬件寄存器(如状态寄存器)   假设我们有一个8位的状态寄存器映射在地址0x1234上。系统需要我们一直监测状态寄存器的值,直到它的值不为0为止。通常错误的实现方法是: UINT1 * ptr = (UINT1 *) 0x1234; // Wait for register to become non-zero.等待寄存器为非0值 while (*ptr == 0); // Do something else.作其他事情 汇编代码:     mov ptr, #0x1234     mov a, @ptr     loop bz loop  编译器认为变量值总是不变的,不必要到内存中读取它,使用一个无限循环来结束 正确:     UINT1 volatile * ptr =     (UINT1 volatile *) 0x1234

Does Interlocked guarantee visibility to other threads in C# or do I still have to use volatile?

情到浓时终转凉″ 提交于 2019-12-04 22:06:46
问题 I've been reading the answer to a similar question, but I'm still a little confused... Abel had a great answer, but this is the part that I'm unsure about: ...declaring a variable volatile makes it volatile for every single access. It is impossible to force this behavior any other way, hence volatile cannot be replaced with Interlocked. This is needed in scenarios where other libraries, interfaces or hardware can access your variable and update it anytime, or need the most recent version.

Global Variable Access Relative to Function Calls and Returns

混江龙づ霸主 提交于 2019-12-04 21:15:46
I have been researching this topic and I can not find a specific authoritative answer. I am hoping that someone very familiar with the C spec can answer - i.e. confirm or refute my assertion, preferably with citation to the spec. Assertion: If a program consists of more than one compilation unit (separately compiled source file), the compiler must assure that global variables (if modified) are written to memory before any call to a function in another unit or before the return from any function. Also, in any function, the global must be read before its first use. Also after a call of any

多线程之美1一volatile

假装没事ソ 提交于 2019-12-04 21:12:28
摘自: https://www.cnblogs.com/flydashpig/p/11875652.html 多线程之美1一volatile 目录 一、java内存模型 1.1、抽象结构图 1.2、概念介绍 二、volatile详解 2.1、概念 2.2、保证内存可见性 2.3、不保证原子性 2.4、有序性 一、java内存模型 1.1、抽象结构图 1.2、概念介绍 java 内存模型 即Java memory model(简称JMM), java线程之间的通信由JMM控制,决定一个线程对共享变量的写入何时对另一个线程可见。 多线程通信通常分为2类:共享内存和消息传递 JMM采用的就是共享内存来实现线程间的通信,且通信是隐式的,对程序开发人员是透明的,所以在了解其原理了,才会对线程之间通信,同步,内存可见性问题有进一步认识,避免开发中出错。 线程之间如何通信? 在java中多个线程之间要想通信,如上图所示,每个线程在需要操作某个共享变量时,会将该主内存中这个共享变量拷贝一份副本存在在自己的本地内存(也叫工作内存,这里只是JMM的一个抽象概念,即将其笼统看做一片内存区域,用于每个线程存放变量,实际涉及到缓存,寄存器和其他硬件),线程操作这个副本,比如 int i = 1;一个线程想要进行 i++操作,会先将变量 i =1 的值先拷贝到自己本地内存操作,完成 i++,结果 i=2

is volatile of no use on x86 processors

不想你离开。 提交于 2019-12-04 20:54:22
I read somewhere that x86 processors have cache coherency and can sync the value of fields across multiple cores anyway on each write. Does that mean that we can code without using the 'volatile' keywoard in java if we plan on running only on x86 processors? Update: Ok assuming that we leave out the issue of instruction reordering, can we assume that the issue of an assignment to a non-volatile field not being visible across cores is not present on x86 processors? No -- the volatile keyword has more implications than just cache coherency; it also places restrictions on what the runtime can and

Java: how volatile guarantee visibility of “data” in this piece of code?

本秂侑毒 提交于 2019-12-04 18:56:36
Class Future { private volatile boolean ready; private Object data; public Object get() { if(!ready) return null; return data; } public synchronized void setOnce(Object o) { if(ready) throw...; data = o; ready = true; } } It said "if a thread reads data, there is a happens-before edge from write to read of that guarantees visibility of data" I know from my learning: volatile ensures that every read/write will be in the memory instead of only in cache or registers; volatile ensures reorder: that is, in setOnce() method data = o can only be scheduled after if(ready) throw..., and before ready =

java volatile的 store load相关指令

给你一囗甜甜゛ 提交于 2019-12-04 18:43:51
java中的volatile,从网上很多资料来看,保证了被修饰变量的可见性以及有序性 对于这个有序性,是通过编译时候生成对应的内存屏障来保证不会被重排序。而这个内存屏障对应的指令码有以下4中: storestore, storeload, loadload, loadstore 这种xy形式的指令,其语义如下:在xy之前的x操作,不能与xy之后的y操作进行重排序。 对于读的情况,加入的指令如下 loadload load 读操作 loadstore 对于写,如下所示 storestore store 写操作 storeload 根据以上的两种类型,来仔细查看两种操作的4个组合:读读,读写,写读,写写 读读,如下。这种情况load1和load2是否会被重排序呢?不会,第4行保证了前后的load操作不能重排序 loadload load1 loadstore loadload load2 loadstore  读写,如下。也不会被重排序,第3行的loadstore保证了。 loadload load1 loadstore storestore store1 storeload 写读,如下。不会被重排序,第3行的storeload保证了。 storestore store1 storeload loadload load1 loadstore 写写,如下。不会被重排序

volatile 关键字特性解析及单例模式下的使用

我是研究僧i 提交于 2019-12-04 18:28:25
一、什么是 volatile ? volatile 是 Java 中的一个关键字,Java 虚拟机提供的轻量级同步机制。 二、JMM(Java Memory Model) 为了更好的理解 volatile 关键字,应该了解了解 JMM。 JMM(Java内存模型Java Memory Model,简称 JMM)本身是一种抽象的概念并不真实存在,它描述的是一组规则或规范通过规范定制了程序中各个变量(包括实例字段,静态字段和构成数组对象的元素)的访问方式。 JMM 的特点: 1、可见性 2、原子性 3、有序性 JMM关于同步规定: 1、线程解锁前,必须把共享变量的值刷新回主内存 2、线程加锁前,必须读取主内存的最新值到自己的工作内存 3、加锁解锁是同一把锁 由于JVM运行程序的实体是线程,而每个线程创建时JVM都会为其创建一个工作内存(有些地方成为栈空间),工作内存是每个线程的私有数据区域,而Java内存模型中规定所有变量都存储在主内存,主内存是共享内存区域,所有线程都可访问, 但线程对变量的操作(读取赋值等)必须在工作内存中进行,首先要将变量从主内存拷贝到自己的工作空间,然后对变量进行操作,操作完成再将变量写回主内存 ,不能直接操作主内存中的变量,各个线程中的工作内存储存着主内存中的变量副本拷贝,因此不同的线程无法访问对方的工作内存,此案成间的通讯(传值) 必须通过主内存来完成

What is meant by “fast-path” uncontended synchronization?

爷,独闯天下 提交于 2019-12-04 18:19:15
问题 From the Performance and Scalability chapter of the JCIP book: The synchronized mechanism is optimized for the uncontended case(volatile is always uncontended), and at this writing, the performance cost of a "fast-path" uncontended synchronization ranges from 20 to 250 clock cycles for most systems. What does the author mean by fast-path uncontended synchronization here? 回答1: I'm not familiar with the topic of the book, but in general a “fast path” is a specific possible control flow branch