volatile

java并发之内存模型

百般思念 提交于 2019-12-01 07:03:24
java内存模型知识导图 一 并发问题及含义 并发编程存在原子性、可见性、有序性问题。 原子性即一系列操作要么都执行,要么都不执行。 可见性,一个线程对共享变量的修改,另一个线程可能不会马上看到。由于多核CPU,每个CPU核都有高速缓存,会缓存共享变量,某个线程对共享变量的修改会改变高速缓存中的值,但却不会马上写入内存。另一个线程读到的是另一个核缓存的共享变量的值,出现缓存不一致问题。 有序性,即程序执行的顺序按照代码的先后顺序执行。编译器和处理器会对指令进行重排,以优化指令执行性能,重排不会改变单线程执行结果,但在多线程中可能会引起各种各样的问题。 二 内存模型 为了保证共享内存的正确性(可见性、有序性、原子性),内存模型定义了共享内存系统中多线程程序读写操作行为的规范。内存模型解决并发问题 主要采用两种方式:限制处理器优化和使用内存屏障。 顺序一致性内存模型是一种理论参考模型,提供了极强的内存可见性保证,具有两大特性: 一个线程的所有操作按照程序的顺序执行,而不能重排序。 所有线程只能看到单一的执行顺序。每个操作都必须原子执行且立刻对其它线程可见。 顺序一致性内存模型禁止很多处理器和编译器重排,影响执行性能,处理器内存模型和JMM对顺序一致性内存模型进行放松,执行性能:处理器内存模型>JMM>顺序一致性内存模型,易编程性:处理器内存模型<JMM<顺序一致性内存模型。 三

Volatile and compiler optimization

≡放荡痞女 提交于 2019-12-01 06:45:35
Is it OK to say that 'volatile' keyword makes no difference if the compiler optimization is turned off i.e (gcc -o0 ....)? I had made some sample 'C' program and seeing the difference between volatile and non-volatile in the generated assembly code only when the compiler optimization is turned on i.e ((gcc -o1 ....). No, there is no basis for making such a statement. volatile has specific semantics that are spelled out in the standard. You are asserting that gcc -O0 always generates code such that every variable -- volatile or not -- conforms to those semantics. This is not guaranteed; even if

Is this a better version of Double Check Locking without volatile and synchronization overhead

一世执手 提交于 2019-12-01 05:55:45
Below code snippet is from Effective Java 2nd Edition Double Checked Locking // Double-check idiom for lazy initialization of instance fields private volatile FieldType field; FieldType getField() { FieldType result = field; if (result == null) { // First check (no locking) synchronized(this) { result = field; if (result == null)// Second check (with locking) field = result = computeFieldValue(); } } return result; } From what i know the main Problem with Double Checked Locking is the reordering inside second check locking so that the other thread might see the values of field/result as set

Why are unnecessary atomic loads not optimized away?

爷,独闯天下 提交于 2019-12-01 05:13:55
Let's consider this trivial code: #include <atomic> std::atomic<int> a; void f(){ for(int k=0;k<100;++k) a.load(std::memory_order_relaxed); } MSVC, Clang and GCC all perform 100 loads of a, while it seems obvious it could have been optimized away. I expected the function f to be a nop (See generated code here ) Actually, I expected this code generation for a volatile atomic: volatile std::atomic<int> va; void g(){ for(int k=0;k<100;++k) va.load(std::memory_order_relaxed); } Why do compilers not optimize away unnecessary atomic loads? 来源: https://stackoverflow.com/questions/56046501/why-are

Java内存模型的简单概括

北城余情 提交于 2019-12-01 04:54:26
java内存模型是java虚拟机规范中定义的关于java程序中变量的访问规则,主要目标是为了屏蔽不同硬件和操作系统对于变量访问方式的差异,在java虚拟机层面保持相同的变量访问的语义。 java内存模型把内存分为主内存和工作内存,主内存是虚拟机内存的一部分可以认为是Java堆,工作内存是java虚拟机中每个线程自己的工作内存,可以认为是线程的栈空间。 java虚拟机为工作内存和主内存的交互定义8种操作,8种操作之间要遵循一定的规则。 java内存模型主要围绕着并发过程中,原子性、可见性、有序性三个特征来设计的。 原子性主要通过是通过原子性的变量操作和锁机制来完成的。 可见性主要通过volatile、synchronized和final来实现。 有序性主要是通过volatile和synchronized来保证,java语言中也自带了先行发生原则,也叫happens-before原则。 happens-before原则主要包括程序次序原则、管程锁定规则、volatile变量规则、线程启动规则、线程终止规则、线程中断规则、对象终结规则、传递性。 来源: https://www.cnblogs.com/liuyu7177/p/11655946.html

Why access volatile variable is about 100 slower than member?

☆樱花仙子☆ 提交于 2019-12-01 04:21:16
Here I wrote a test about access speed of local, member, volatile member: public class VolatileTest { public int member = -100; public volatile int volatileMember = -100; public static void main(String[] args) { int testloop = 10; for (int i = 1; i <= testloop; i++) { System.out.println("Round:" + i); VolatileTest vt = new VolatileTest(); vt.runTest(); System.out.println(); } } public void runTest() { int local = -100; int loop = 1; int loop2 = Integer.MAX_VALUE; long startTime; startTime = System.currentTimeMillis(); for (int i = 0; i < loop; i++) { for (int j = 0; j < loop2; j++) { } for

Visual C++ Volatile

﹥>﹥吖頭↗ 提交于 2019-12-01 03:46:06
The MSDN docs for "volatile" in Visual C++ indicate that writes have "release semantics" and that reads have "acquire semantics", in addition to ensuring that reads always read from memory and that writes always write accordingly. The C spec for "volatile" includes the second part (don't do crazy optimizations), but not the first part (a memory fence). Is there any way in Visual C++ to get the "C" volatile behaviour only, without the memory fence? I want to force a variable to always be on the stack, in a fixed spot, but I don't want to take the overhead of a memory fence on every assignment

How can I safely iterate a lua table while keys are being removed

▼魔方 西西 提交于 2019-12-01 03:39:00
In my main coroutine, I am removing or adding entries from a table depending on user operations. In the background, I'd like to iterate over the entries in the table. I don't mind particularly if I miss an insertion on one iteration, providing I can catch it before the next. Is it safe to iterate over it with pairs ? Or should I use next instead? You can safely remove entries while traversing a table but you cannot create new entries, that is, new keys. You can modify the values of existing entries, though. (Removing an entry being a special case of that rule.) JUST MY correct OPINION You can

Correct behaviour of trivial statements involving expressions with volatile variables?

送分小仙女□ 提交于 2019-12-01 03:33:30
Consider the following statements volatile int a = 7; a; // statement A volatile int* b = &a; *b; // statement B volatile int& c = a; c; // statement C Now, I've been trying to find a point in the standard that tells me how a compiler is to behave when coming across these statements. All I could find is that A (and possibly C) gives me an lvalue, and so does B: "§ 5.1.1.8 Primary expressions - General" says An identifier is an id-expression provided it has been suitably declared (Clause 7). [..] [..] The result is the entity denoted by the identifier. The result is an lvalue if the entity is a

Is this a better version of Double Check Locking without volatile and synchronization overhead

Deadly 提交于 2019-12-01 03:30:53
问题 Below code snippet is from Effective Java 2nd Edition Double Checked Locking // Double-check idiom for lazy initialization of instance fields private volatile FieldType field; FieldType getField() { FieldType result = field; if (result == null) { // First check (no locking) synchronized(this) { result = field; if (result == null)// Second check (with locking) field = result = computeFieldValue(); } } return result; } From what i know the main Problem with Double Checked Locking is the