volatile

CAS--JAVA为什么选择它,如何解析并实现它?

纵饮孤独 提交于 2019-12-20 02:44:38
这边参考的是: A-B-A问题: https://hesey.wang/2011/09/resolve-aba-by-atomicstampedreference.html CAS解析: https://blog.csdn.net/v123411739/article/details/79561458 https://www.cnblogs.com/onlywujun/articles/3529572.html 牛人: http://ifeve.com/ 首先CAS 是是什么? Compare and Swap,是比较并交换的意思。 CAS有3个操作数,内存值V,旧的预期值A,要修改的新值B。当且仅当预期值A和内存值V相同时,将内存值V修改为B,否则什么都不做。 它是一种非阻塞型的算法,一个线程的失败或者挂起不应该影响其他线程的失败或挂起的算法。 现代的CPU提供了特殊的指令,可以自动更新共享数据,而且能够检测到其他线程的干扰,而 compareAndSet() 就用这些代替了锁定。 这边拿AtomicInteger来研究在没有锁的情况下是如何做到数据正确性的。 private volatile int value; 在没有锁的机制下可能需要借助volatile原语,保证线程间的数据是可见的(共享的)。 这样在获取变量的值的时候才能直接读取。 public final int

并发中关键字的语义

放肆的年华 提交于 2019-12-19 21:08:49
一、volatile的内存语义 1. 简单的举例 可以把对volatile变量的单个读/写,看成是使用同一个锁对这些单个读/写操作做了同步; 但是复合操作是不生效的; package com.youyou.ch1.demo; public class Vola { volatile int a = 1; //使用volatile 声明int类型的变量 public int getA() { return a; //对单个volatile 变量进行读 } public void setA(int a) { this.a = a; //对单个volatile 变量进行写 } public void inc() { a++; //对复合(多个)volatile 变量进行 读/写 } } View Code 这样的代用 volatile 关键字的代码和下面的是一样的; package com.youyou.ch1.demo; public class VolaLikeSyn { int a = 0; //普通的变量 public synchronized int getA() { return a; //对单个普通 变量进行读 } public synchronized void setA(int a) { this.a = a; //对单个普通 变量进行写 } public void

Code example proven to fail w/o volatile

a 夏天 提交于 2019-12-19 10:22:36
问题 Below is a C# code example which is a verbatim translation of a broken Java code (which has proven to break (i. e. the 2nd thread may fail to observe the change of sharedValue value) at least on Mac OS X 10.9, Java 1.8 (64 bit), Arrandale (1 socket x 2 core x 2 HT = 4 HW threads)): using System; using System.Threading; class ThreadTest { /* volatile */ private int sharedValue; private void RunAsync() { while (this.sharedValue == 0); } private bool Test() { Thread t = new Thread(this.RunAsync)

C#的一些关键字的总结

百般思念 提交于 2019-12-19 06:56:24
今天突然有一种整理一下C#关键字的冲动,就转化为行动了! C# 关键字完整列表 abstract as base bool break byte case catch char checked class const continue decimal default delegate do double else enum ecent explicit extern false finally fixed float for foreach get goto if implicit in int interface internal is lock long namespace new null object out override partial private protected public readonly ref return sbyte sealed set short sizeof stackalloc static struct switch this throw true try typeof uint ulong unchecked unsafe ushort using value virtual volatile volatile void where while yield 其中有几个比较容易弄错的 关键字 描 述 abstract 可以和类、方法、属性

Correct behaviour of trivial statements involving expressions with volatile variables?

你说的曾经没有我的故事 提交于 2019-12-19 05:45:19
问题 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). [

Java volatile for concurrency

♀尐吖头ヾ 提交于 2019-12-19 04:41:10
问题 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

Should my Scala actors' properties be marked @volatile?

﹥>﹥吖頭↗ 提交于 2019-12-18 16:27:18
问题 In Scala, if I have a simple class as follows: val calc = actor { var sum = 0 loop { react { case Add(n) => sum += n case RequestSum => sender ! sum } } } Should my field sum be marked @volatile ? Whilst the actor is logically single-threaded (i.e. the messages are processed sequentially), the individual reactions may be happening on separate threads and hence the state variable may be being altered on one thread and then read from another. 回答1: You don't need to mark them as volatile. The

What is extern volatile pointer

拜拜、爱过 提交于 2019-12-18 15:17:46
问题 What is extern volatile pointer. extern volatile uint32 *ptr; Here, what will be the behavior of *ptr? What does this actually mean? And, when it should be used? I have tried to google about it, but didn't get any satisfactory answer, there is not much info present about this combination. 回答1: Both extern and volatile keywords can be considered independently. The role of each of these keywords does not interact with the other one, and thus an explanation of each of them can be detailed

并发容器之ConcurrentHashMap

邮差的信 提交于 2019-12-18 13:52:12
  JDK5中添加了新的concurrent包,相对同步容器而言,并发容器通过一些机制改进了并发性能。因为同步容器将所有对容器状态的访问都 串行化了,这样保证了线程的安全性,所以这种方法的代价就是严重降低了并发性,当多个线程竞争容器时,吞吐量严重降低。因此Java5.0开 始针对多线程并发访问设计,提供了并发性能较好的并发容器,引入了java.util.concurrent包。与Vector和Hashtable、 Collections.synchronizedXxx()同步容器等相比,util.concurrent中引入的并发容器主要解决了两个问题: 1)根据具体场景进行设计,尽量避免synchronized,提供并发性。 2)定义了一些并发安全的复合操作,并且保证并发环境下的迭代操作不会出错。   util.concurrent中容器在迭代时,可以不封装在synchronized中,可以保证不抛异常,但是未必每次看到的都是"最新的、当前的"数据。   下面是对并发容器的简单介绍:   ConcurrentHashMap代替同步的Map(Collections.synchronized(new HashMap())),众所周知,HashMap是根据散列值分段存储的,同步Map在同步的时候锁住了所有的段

Volatile and cache behaviour

对着背影说爱祢 提交于 2019-12-18 13:38:18
问题 I read post C volatile variables and Cache Memory But i am confused. Question: whether OS will take care itself OR programmer has to write program in such a way that variable should not go into cache as mention like declaring variable as _Uncached. Regards Learner 回答1: To clarify: volatile is a C concept and tells the compiler to fetch a variable each time from memory rather then use a "compiler-generated" cached version in registers or optimise certain code. What may be causing confusion