volatile

Why are unnecessary atomic loads not optimized away?

ぐ巨炮叔叔 提交于 2019-12-01 03:13:07
问题 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

Why access volatile variable is about 100 slower than member?

可紊 提交于 2019-12-01 02:21:45
问题 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 =

Java volatile for concurrency

送分小仙女□ 提交于 2019-12-01 01:11:54
Ok so I just read this question Do you ever use the volatile keyword in Java? , and I get using a volatile variable in order to stop a loop. Also I've seen this reference, http://www.javamex.com/tutorials/synchronization_volatile.shtml . Now the article says that volatile variables are non-blocking. Also it says that it cannot be used for concurrency in a read-update-write sequence. Which makes sense because they're non-blocking. Since volatile variables are never cached is it faster to simply use synchronization to stop the loop (from the earlier link)? Edit: Using a synchronized solution

如何正确地写出单例模式

萝らか妹 提交于 2019-12-01 00:54:00
懒汉式,线程不安全 懒汉式,线程安全 双重检验锁 饿汉式 static final field 静态内部类 static nested class 枚举 Enum 总结 Read More 单例模式算是设计模式中最容易理解,也是最容易手写代码的模式了吧。但是其中的坑却不少,所以也常作为面试题来考。本文主要对几种单例写法的整理,并分析其优缺点。很多都是一些老生常谈的问题,但如果你不知道如何创建一个线程安全的单例,不知道什么是双检锁,那这篇文章可能会帮助到你。 懒汉式,线程不安全 当被问到要实现一个单例模式时,很多人的第一反应是写出如下的代码,包括教科书上也是这样教我们的。 public class Singleton { private static Singleton instance; private Singleton (){} public static Singleton getInstance() { if (instance == null) { instance = new Singleton(); } return instance; } } 这段代码简单明了,而且使用了懒加载模式,但是却存在致命的问题。当有多个线程并行调用 getInstance() 的时候,就会创建多个实例。也就是说在多线程下不能正常工作。 懒汉式,线程安全 为了解决上面的问题

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

扶醉桌前 提交于 2019-12-01 00:25:50
问题 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? 回答1: 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

如何正确地写出单例模式

你说的曾经没有我的故事 提交于 2019-12-01 00:16:00
懒汉式,线程不安全 懒汉式,线程安全 双重检验锁 饿汉式 static final field 静态内部类 static nested class 枚举 Enum 总结 Read More 单例模式算是设计模式中最容易理解,也是最容易手写代码的模式了吧。但是其中的坑却不少,所以也常作为面试题来考。本文主要对几种单例写法的整理,并分析其优缺点。很多都是一些老生常谈的问题,但如果你不知道如何创建一个线程安全的单例,不知道什么是双检锁,那这篇文章可能会帮助到你。 懒汉式,线程不安全 当被问到要实现一个单例模式时,很多人的第一反应是写出如下的代码,包括教科书上也是这样教我们的。 public class Singleton { private static Singleton instance; private Singleton (){} public static Singleton getInstance() { if (instance == null) { instance = new Singleton(); } return instance; } } 这段代码简单明了,而且使用了懒加载模式,但是却存在致命的问题。当有多个线程并行调用 getInstance() 的时候,就会创建多个实例。也就是说在多线程下不能正常工作。 懒汉式,线程安全 为了解决上面的问题

如何正确地写出单例模式

白昼怎懂夜的黑 提交于 2019-12-01 00:15:17
懒汉式,线程不安全 懒汉式,线程安全 双重检验锁 饿汉式 static final field 静态内部类 static nested class 枚举 Enum 总结 Read More 单例模式算是设计模式中最容易理解,也是最容易手写代码的模式了吧。但是其中的坑却不少,所以也常作为面试题来考。本文主要对几种单例写法的整理,并分析其优缺点。很多都是一些老生常谈的问题,但如果你不知道如何创建一个线程安全的单例,不知道什么是双检锁,那这篇文章可能会帮助到你。 懒汉式,线程不安全 当被问到要实现一个单例模式时,很多人的第一反应是写出如下的代码,包括教科书上也是这样教我们的。 public class Singleton { private static Singleton instance; private Singleton (){} public static Singleton getInstance() { if (instance == null) { instance = new Singleton(); } return instance; } } 这段代码简单明了,而且使用了懒加载模式,但是却存在致命的问题。当有多个线程并行调用 getInstance() 的时候,就会创建多个实例。也就是说在多线程下不能正常工作。 懒汉式,线程安全 为了解决上面的问题

如何正确地写出单例模式

爱⌒轻易说出口 提交于 2019-12-01 00:05:49
懒汉式,线程不安全 懒汉式,线程安全 双重检验锁 饿汉式 static final field 静态内部类 static nested class 枚举 Enum 总结 Read More 单例模式算是设计模式中最容易理解,也是最容易手写代码的模式了吧。但是其中的坑却不少,所以也常作为面试题来考。本文主要对几种单例写法的整理,并分析其优缺点。很多都是一些老生常谈的问题,但如果你不知道如何创建一个线程安全的单例,不知道什么是双检锁,那这篇文章可能会帮助到你。 懒汉式,线程不安全 当被问到要实现一个单例模式时,很多人的第一反应是写出如下的代码,包括教科书上也是这样教我们的。 public class Singleton { private static Singleton instance; private Singleton (){} public static Singleton getInstance() { if (instance == null) { instance = new Singleton(); } return instance; } } 这段代码简单明了,而且使用了懒加载模式,但是却存在致命的问题。当有多个线程并行调用 getInstance() 的时候,就会创建多个实例。也就是说在多线程下不能正常工作。 懒汉式,线程安全 为了解决上面的问题

浅谈Java的内存模型以及交互

北战南征 提交于 2019-11-30 23:50:25
本文的内存模型只写虚拟机内存模型,物理机的不予描述。 Java内存模型   在Java中,虚拟机将运行时区域分成6中,如下图:      程序计数器: 用来记录当前线程执行到哪一步操作。在多线程轮换的模式中,当当前线程时间片用完的时候记录当前操作到哪一步,重新获得时间片时根据此记录来恢复之前的操作。 虚拟机栈: 这就是我们平时所说的栈了,一般用来储存局部变量表、操作数表、动态链接等。 本地方法栈: 这是另一个栈,用来提供虚拟机中用到的本地服务,像线程中的start方法,JUC包里经常使用的CAS等方法都是从这来的。 堆: 主要的储存区域,平时所创建的对象都是放在这个区域。其内部还分为新生代、老年代和永久代(也就是方法区,在Java8之后删除了),新生代又分为两块Survivor和一块Eden,平时创建的对象其实都是在Eden区创建的,不过这些之后再跟垃圾回收器写在一篇文章。 方法区: 储存符号引用、被JVM加载的类信息、静态变量的地方。在Java8之后方法区被移除,使用元空间来存放类信息,常量池和其他东西被移到堆中(其实在7的时候常量池和静态变量就已经被移到堆中),不再有永久代一说。删除的原因大致如下: 容易造成内存溢出或内存泄漏,例如 web开发中JSP页面较多的情况。 由于类和方法的信息难以确定,不好设定大小,太大则影响年老代,太小容易内存溢出。 GC不好处理,回收效率低下

Uses of volatile without synchronization

笑着哭i 提交于 2019-11-30 23:48:56
问题 Knowing that Reads and writes are atomic for all variables declared volatile Question1: Can this be understood as if private volatile int x = 0; x++; operation is atomic? And that Marking variable volatile does not eliminate all need to synchronize atomic actions, because memory consistency errors are still possible. Question2: I wonder under what circumstances (if any) it is possible to see a variable marked volatile and not see any methods of blocks marked synchronized (that attempt to