volatile

并发(JMM综述)

核能气质少年 提交于 2019-12-10 15:31:41
JMM综述 文章目录 JMM综述 一,内存模型产生背景 二,内存模型概念 2.1JMM组成部分 2.2JVM内存操作的并发问题 2.3内存交互操作流程 三,JMM深入 3.1原子性,可见性和有序性 3.2happens-before 3.3内存屏障 3.3重排序 四,volatile 4.1volatile内存语义 4.2volatile特性 4.3volatile如何禁止指令重排序 五,synchronize 六,final 一,内存模型产生背景 由于计算机的存储设备与处理器的运算速度有几个数量级的差距,为了避免处理器等待缓慢的内存读写操作完成,现代计算机系统通过加入一层读写速度尽可能无限接近处理器运算速度的高速缓存 缓存作为内存和cpu之间的缓冲区,将运算需要用到的数据放入到高速缓存中,让运算能快速运行,运算结束后在从缓存同步回内存之中 类似redis缓存和数据库一样,引入了高速缓存虽然解决了处理器和内存速度的差异,但是却带来一个新的问题-----缓存一致性问题 在多核处理器的系统中,每个处理器都有自己的高速缓存,他们共享同一内存,当多个处理器的运算任务都涉及同一块内存区域时,可能会出现缓存数据不一致的问题,需要使用缓存一致性协议来维护缓存的一致性 二,内存模型概念 java虚拟机提供java内存模型(JMM)来 屏蔽掉各种硬件和操作系统的内存访问差异 ,以实现

C#: volatile reads and writes of HashSet

冷暖自知 提交于 2019-12-10 14:50:07
问题 I have a class: public class Checker { private HashSet<int> _hs = new HashSet<int>(); public bool Check(int a) { return Volatile.Read(ref _hs).Contains(a); } public void Update(IEnumerable<int> items) { Volatile.Write(ref _hs, new HashSet<int>(items)); } } Method Check is called from multiple threads quite often. Method Update is called from a single thread which monitors some source (database, http service etc.). Is this pattern of Volatile.Read / Volatile.Write usage correct? 回答1: If you

C++ - Should data passed to a thread be volatile?

吃可爱长大的小学妹 提交于 2019-12-10 14:49:59
问题 In Microsoft Visual C++ I can call CreateThread() to create a thread by starting a function with one void * parameter. I pass a pointer to a struct as that parameter, and I see a lot of other people do that as well. My question is if I am passing a pointer to my struct how do I know if the structure members have been actually written to memory before CreateThread() was called? Is there any guarantee they won't be just cached? For example: struct bigapple { string color; int count; } apple;

Android volatile not working?

岁酱吖の 提交于 2019-12-10 14:13:06
问题 I have an Activity class, in which I have a static flag, let's say public static volatile flag = false; Then in the class, I start a thread, which checks the flag and do different things. I also have a broadcastreceiver, which sets the flag to true or false. I though volatile will force the flag to the most recent value. But I can see my broadcastreceiver sets the static flag to true, but my thread is still getting it as false. Am I missing something basic here? Any help would be appreciated!

Example C code that demonstrates volatile in disassembly?

China☆狼群 提交于 2019-12-10 13:45:37
问题 What is a short illustrative C program that demonstrates the difference between volatile and non-volatile in the disassembly? ie int main() { volatile int x; ??? } vs int main() { int x; ??? } What can we replace both ??? with such that the generated code is different? 回答1: for example: x = 0; If x is not volatile , the compiler will see that it's unused and will probably eliminate it (either the x = 0; statement or even the variable itself) completely from the generated code as an

Is volatile needed when variable is only read during interrupt

六月ゝ 毕业季﹏ 提交于 2019-12-10 12:45:25
问题 The C standard states that the volatile keyword should be used in the definition of a variable when there's a chance that the variable's value could change outside the normal flow of execution of the program. If a global variable is changed (written) during normal execution flow and only read outside this normal flow (in an interrupt). Does this variable need to be volatile ? And why ? 回答1: If a global variable is changed (written) during normal execution flow and only read outside this

并发文章

扶醉桌前 提交于 2019-12-10 12:17:50
并发文章 Java Volatile关键字 https://ifeve.com/java-volatile%e5%85%b3%e9%94%ae%e5%ad%97/ 来源: https://www.cnblogs.com/mozq/p/12015536.html

When Java refresh Thread Cache to actual copy

☆樱花仙子☆ 提交于 2019-12-10 12:17:21
问题 I read few articles on volatile Thread cache and found either it is too much brief without examples, so it is very difficult for beginner to understand. Please help me in understanding below program, public class Test { int a = 0; public static void main(String[] args) { final Test t = new Test(); new Thread(new Runnable(){ public void run() { try { Thread.sleep(3000); } catch (Exception e) {} t.a = 10; System.out.println("now t.a == 10"); } }).start(); new Thread(new Runnable(){ public void

Garbage collection and synchronized visibility

[亡魂溺海] 提交于 2019-12-10 11:46:43
问题 I have read about marking an object as volatile doesn't guarantee visibility of it's members ( I'm not saying about thread safety just memory visibility , quoting : only the object reference will be considered to be volatile by the JVM and not the object data itself which will reside on the heap my questions : The synchronize will ensure the visibility of the members (on the same lock object) in case they have been edited. Is that because the happens-before at the end (release) of the lock

Control of running Thread using multiple threading concept of java

末鹿安然 提交于 2019-12-10 11:28:58
问题 I just want to start and stop the thread when return key is pressed. Here thread is stopped fine but i cant Start that thread again please help. Also explain me the use of volatile keyword .Is it helpful for me to over come this problem. public class Sync extends Thread{ public boolean meth= true; public void run(){ while(meth){ System.out.println("hello"); try { Thread.sleep(1000); } catch (InterruptedException ex) { } } } public void shutdown(){ meth=false; } public void startup(){ meth