volatile

易失性与联锁对抗锁定

喜你入骨 提交于 2019-12-29 19:40:19
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 假设一个类有一个由多个线程访问的 public int counter 字段。 此 int 仅递增或递减。 要增加此字段,应使用哪种方法,为什么? lock(this.locker) this.counter++; , Interlocked.Increment(ref this.counter); , 将 counter 的访问修饰符更改为 public volatile 。 既然我发现了 volatile ,我一直在删除许多 lock 语句和 Interlocked 的使用。 但是有理由不这样做吗? #1楼 我是第二个Jon Skeet的回答,并希望为想要了解更多关于“volatile”和Interlocked的人们添加以下链接: 原子性,波动性和不变性是不同的,第一部分 - (Eric Lippert的编码中的神话般的冒险) 原子性,波动性和不变性是不同的,第二部分 原子性,波动性和不变性是不同的,第三部分 Sayonara Volatile - (2012年出现的Joe Duffy博客的Wayback Machine快照) #2楼 互锁功能不会锁定。 它们是原子的,这意味着它们可以完成而不会在增量期间进行上下文切换。 所以没有死锁或等待的可能性。 我会说你应该总是喜欢锁定和增量。

AtomicInteger and volatile [duplicate]

回眸只為那壹抹淺笑 提交于 2019-12-29 16:07:12
问题 This question already has answers here : What is the difference between atomic / volatile / synchronized? (7 answers) Closed 3 months ago . I know volatile allows for visibility, AtomicInteger allows for atomicity. So if I use a volatile AtomicInteger , does it mean I don't have to use any more synchronization mechanisms? Eg. class A { private volatile AtomicInteger count; void someMethod(){ // do something if(count.get() < 10) { count.incrementAndGet(); } } Is this threadsafe? 回答1: I believe

benchmarking, code reordering, volatile

浪子不回头ぞ 提交于 2019-12-29 14:23:18
问题 I decide I want to benchmark a particular function, so I naïvely write code like this: #include <ctime> #include <iostream> int SlowCalculation(int input) { ... } int main() { std::cout << "Benchmark running..." << std::endl; std::clock_t start = std::clock(); int answer = SlowCalculation(42); std::clock_t stop = std::clock(); double delta = (stop - start) * 1.0 / CLOCKS_PER_SEC; std::cout << "Benchmark took " << delta << " seconds, and the answer was " << answer << '.' << std::endl; return 0

benchmarking, code reordering, volatile

萝らか妹 提交于 2019-12-29 14:21:05
问题 I decide I want to benchmark a particular function, so I naïvely write code like this: #include <ctime> #include <iostream> int SlowCalculation(int input) { ... } int main() { std::cout << "Benchmark running..." << std::endl; std::clock_t start = std::clock(); int answer = SlowCalculation(42); std::clock_t stop = std::clock(); double delta = (stop - start) * 1.0 / CLOCKS_PER_SEC; std::cout << "Benchmark took " << delta << " seconds, and the answer was " << answer << '.' << std::endl; return 0

illustrating volatile : is this code thread-safe?

佐手、 提交于 2019-12-29 06:33:08
问题 I'm trying to illustrate the use and importance of volatile with an example that would really not give a good result if volatile was omitted. But I'm not really used to using volatile . The idea of the following code is to cause an infinite loop if volatile is omitted, and be perfectly thread-safe if volatile is present. Is the following code thread-safe? Do you have any other realistic and short example of code that uses volatile and would give an obviously incorrect result without it? Here

Java内存模型之happens-before

梦想与她 提交于 2019-12-29 03:44:21
无论处理器、JVM、编译器都会都保证程序正确的前提下尽可能的对指令执行效率进行优化,进行指令重排等操作。而要保证程序的执行结果的正确,则必须要遵循JMM中规定的happens-before原则。 在Java内存模型(JMM)中,如果一个操作的执行结果需要对另一个操作可见,那么两个操作必须要存在happens-before关系。happens-before原则非常重要,它是判断数据是否存在竞争,线程是否安全的主要依据,保证了多线程环境下的可见性,依据这个原则可以解决并发操作可能存在的所有冲突问题。 happens-before原则主要包含单线程程序次序规则,锁定规则,volatile变量可见性规则,传递性规则,线程启动、中断、终结join规则,对象的终结规则(初始化完成先发生于它的销毁操作) happens-before原则定义如下: 1. 如果一个操作happens-before另一个操作,那么第一个操作的执行结果将对第二个操作可见,而且第一个操作的执行顺序排在第二个操作之前。 2. 两个操作之间存在happens-before关系,并不意味着一定要按照happens-before原则制定的顺序来执行。如果重排序之后的执行结果与按照happens-before关系来执行的结果一致,那么这种重排序并不非法。 下面是happens-before原则规则: 程序次序规则:一个线程内

Are volatile reads and writes atomic on Windows+VisualC?

南笙酒味 提交于 2019-12-29 03:39:31
问题 There are a couple of questions on this site asking whether using a volatile variable for atomic / multithreaded access is possible: See here, here, or here for example. Now, the C(++) standard conformant answer is obviously no . However, on Windows & Visual C++ compiler, the situation seems not so clear. I have recently answered and cited the official MSDN docs on volatile Microsoft Specific Objects declared as volatile are (...) A write to a volatile object (volatile write) has Release

Java并发(四):happens-before

孤人 提交于 2019-12-29 03:15:14
happens-before 一个操作执行的结果需要对另一个操作可见,那么这两个操作之间必须存在happens-before关系 happen-before原则是JMM中非常重要的原则,它是判断数据是否存在竞争、线程是否安全的主要依据,保证了多线程环境下的可见性。 happens-before原则定义: 1. 如果一个操作happens-before另一个操作,那么第一个操作的执行结果将对第二个操作可见,而且第一个操作的执行顺序排在第二个操作之前。 2. 两个操作之间存在happens-before关系,并不意味着一定要按照happens-before原则制定的顺序来执行。如果重排序之后的执行结果与按照happens-before关系来执行的结果一致,那么这种重排序并不非法。 下面是happens-before规则:   1)一个线程中的每个操作,happens- before 于该线程中的任意后续操作。   2)监视器锁规则:对一个监视器锁的解锁,happens- before 于随后对这个监视器锁的加锁。   3)volatile变量规则:对一个volatile域的写,happens- before 于任意后续对这个volatile域的读。   4)传递性:如果A happens- before B,且B happens- before C,那么A happens-

java内存模型-锁

▼魔方 西西 提交于 2019-12-29 03:13:52
锁的释放-获取建立的 happens before 关系 锁是 java 并发编程中最重要的同步机制。锁除了让临界区互斥执行外,还可以让释放锁的线程向获取同一个锁的线程发送消息。下面是锁释放-获取的示例代码: class MonitorExample { int a = 0; public synchronized void writer() { //1 a++; //2 } //3 public synchronized void reader() { //4 int i = a; //5 …… } //6 } 假设线程 A 执行 writer() 方法,随后线程 B 执行 reader() 方法。根据 happens before 规则,这个过程包含的 happens before 关系可以分为两类: 根据程序次序规则,1 happens before 2, 2 happens before 3; 4 happens before 5, 5 happens before 6。 根据监视器锁规则,3 happens before 4。 根据 happens before 的传递性,2 happens before 5。 上述 happens before 关系的图形化表现形式如下: 在上图中,每一个箭头链接的两个节点,代表了一个 happens before 关系

JAVA内存模型5-锁

只谈情不闲聊 提交于 2019-12-29 03:12:52
锁的释放-获取建立的happens before关系 锁是java并发编程中最重要的同步机制。锁除了让临界区互斥执行外,还可以让释放锁的线程向获取同一个锁的线程发送消息。下面是锁释放-获取的示例代码: class MonitorExample { int a = 0; public synchronized void writer() { //1 a++; //2 } //3 public synchronized void reader() { //4 int i = a; //5 …… } //6 } View Code   假设线程A执行writer()方法,随后线程B执行reader()方法。根据happens -before规则,这个过程包含的happens -before关系可以分为两类: 根据监视器锁规则,3happens before4。 根据程序次序规则,1happens -before2, 2 happens- before 3; 4 happens- before 5, 5 happens -before 6。 根据happens before 的传递性,2 happens before 5。    上述happens -before关系的图形化表现形式如下:   在上图中,每一个箭头链接的两个节点,代表了一个happens before关系