volatile

多线程5一AbstractQueuedSynchronizer源码分析一

走远了吗. 提交于 2019-12-22 09:28:15
AQS的源码分析 <一> 文章目录 前言 1、什么是CAS ? 2、同步器类结构 3、CLH同步队列 4、AQS中静态内部类Node 5、方法分析 5.1、acquire(int arg ) 5.2、release(int arg) 释放锁 6、总结 前言 在多线程环境下,我们一般会对临界区资源(共享资源)进行加锁,释放锁,保证同一时刻最多只有一个线程(独占模式),就如去公共厕所里,在使用一个小房间时会加锁避免自己在使用的时候,别人突然闯进来一样,引起不必要的麻烦,在使用完后,再打开锁,其他人才可使用;还有生产者消费者模型中,线程之间要同步,需要等待和通知机制,来协调线程合作。那么这些是这么实现的?如可重入锁ReentrantLock, 读写锁ReadWriteLock, 信号量 Semaphore, 计数器CountDownLatch,这些都会涉及线程之间的协调同步,那么会有一个抽象的结构,将这些需要共用的功能抽离出来,统一来满足要求吗?我们一起来看看AbstractQueuedSynchronizer 这个抽象类,如何来实现这些功能和其设计的巧妙, 我们能看到Doug lea 大佬在很多地方使用的循环CAS操作(自旋锁)。 1、什么是CAS ? CAS 即 compare and swap 比较并交换, 涉及到三个参数,内存值V, 预期值A, 要修改的新值B,

Defining volatile class object

一个人想着一个人 提交于 2019-12-22 05:25:21
问题 Can the volatile be used for class objects? Like: volatile Myclass className; The problem is that it doesn't compile, everywhere when some method is invoked, the error says: error C2662: 'function' : cannot convert 'this' pointer from 'volatile MyClass' to 'MyCLass &' What is the problem here and how to solve it? EDIT: class Queue { private: struct Data *data; int amount; int size; public: Queue (); ~Queue (); bool volatile push(struct Data element); bool volatile pop(struct Data *element);

How many usage does “volatile” keyword have in C++ function, from grammar perspective?

荒凉一梦 提交于 2019-12-22 04:44:09
问题 I asked this function based on this concept (maybe incorrect?!): Wherever a const can exist, a volatile can exist at the place. class classA { public: const int Foo() const; } Here the first "const" means the return value is const, we can not change it. the second const means "Is Query", this function can not change the member variable and can not call non-const function. Now comes to volatile: I can understand what volatile does on a variable, like "volatile int a;" However I have no idea of

Why Volatile is behaving weirdly

回眸只為那壹抹淺笑 提交于 2019-12-22 04:37:20
问题 I have experience this weird behavior of volatile keyword recently. As far as i know, volatile keyword is applied on to the variable to reflect the changes done on the data of the variable by one thread onto the other thread. volatile keyword prevents caching of the data on the thread. I did a small test........ I used an integer variable named count, and used volatile keyword on it. Then made 2 different threads to increment the variable value to 10000, so the end resultant should be 20000.

memory mapped files and pointers to volatile objects

依然范特西╮ 提交于 2019-12-22 04:01:46
问题 My understanding of the semantics of volatile in C and C++ is that it turns memory access into (observable) side effects. Whenever reading or writing to a memory mapped file (or shared memory) I would expect the the pointer to be volatile qualified, to indicate that this is in fact I/O. (John Regehr wrote a very good article on the semantics of volatile ). Furthermore, I would expect using functions like memcpy() to access shared memory to be incorrect, since the signature suggests the

Does “volatile” guarantee anything at all in portable C code for multi-core systems?

拜拜、爱过 提交于 2019-12-22 03:15:33
问题 After looking at a bunch of other questions and their answers, I get the impression that there is no widespread agreement on what the "volatile" keyword in C means exactly. Even the standard itself does not seem to be clear enough for everyone to agree on what it means. Among other problems: It seems to provide different guarantees depending on your hardware and depending on your compiler. It affects compiler optimizations but not hardware optimizations, so on an advanced processor that does

In C, how do you declare the members of a structure as volatile?

跟風遠走 提交于 2019-12-22 01:37:28
问题 How do you declare a particular member of a struct as volatile? 回答1: Exactly the same as non- struct fields: #include <stdio.h> int main (int c, char *v[]) { struct _a { int a1; volatile int a2; int a3; } a; a.a1 = 1; a.a2 = 2; a.a3 = 3; return 0; } You can mark the entire struct as volatile by using "volatile struct _a {...}" but the method above is for individual fields. 回答2: Should be pretty straight forward according to this article: Finally, if you apply volatile to a struct or union,

std::is_trivially_copyable - Why are volatile scalar types not trivially copyable?

我的未来我决定 提交于 2019-12-22 01:25:57
问题 The current standards for C++17 (and I've observed similar wording for C++11) have very confusing wording for trivially copyable types. I first stumbled upon this problem with the following code (GCC 5.3.0): class TrivialClass {}; std::is_trivially_copyable<int volatile>::value; // 0 std::is_trivially_copyable<TrivialClass volatile>::value; // 1 ?? Making the confusion even worse, I tried checking to see what std::is_trivial had to say about the matter, only being brought to more confusion.

Java 单例真的写对了么?

瘦欲@ 提交于 2019-12-21 23:43:20
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 单例模式是最简单的设计模式,实现也非常“简单”。一直以为我写没有问题,直到被 Coverity 打脸。 1. 暴露问题 前段时间,有段代码被 Coverity 警告了,简化一下代码如下,为了方便后面分析,我在这里标上了一些序号: private static SettingsDbHelper sInst = null; public static SettingsDbHelper getInstance(Context context) { if (sInst == null) { // 1 synchronized (SettingsDbHelper.class) { // 2 SettingsDbHelper inst = sInst; // 3 if (inst == null) { // 4 inst = new SettingsDbHelper(context); // 5 sInst = inst; // 6 } } } return sInst; // 7 } 大家知道,这可是高大上的 Double Checked locking 模式,保证多线程安全,而且高性能的单例实现,比下面的单例实现,“逼格”不知道高到哪里去了: private static SettingsDbHelper sInst

java 里面保留字volatile及其与synchronized的区别

て烟熏妆下的殇ゞ 提交于 2019-12-21 23:43:10
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 最近在读java并发编程相关的书籍,蚂蚁金服团队的杰作,可以好好把java并发相关的内容好好研究一下 要理解volatile和synchronized的区别,首先还是需要来理解下java的内存模型 java内存模型 java中,线程之间的通信是通过 共享内存 的方式,存储在堆中的实例域,静态域以及数组元素都可以在线程间通信。java内存模型控制一个线程对共享变量的改变何时对另一个线程可见。 线程间的共享变量存在主内存中,而对于每一个线程,都有一个私有的工作内存 。工作内存是个虚拟的概念,涵盖了缓存,写缓冲区,寄存器以及其他的硬件和编译器优化,总之就是指线程的本地内存。存在线程本地内存中的变量值对其他线程是不可见的。 如果线程A与线程B之间如要通信的话,必须要经历下面2个步骤,如图所示: 1. 首先,线程A把本地内存A中更新过的共享变量刷新到主内存中去。 2. 然后,线程B到主内存中去读取线程A之前已更新过的共享变量。 关于volatile变量 由于java的内存模型中有工作内存和主内存之分,所以可能会有两种问题: (1)线程可能在工作内存中更改变量的值,而没有及时写回到主内存,其他线程从主内存读取的数据仍然是老数据 (2)线程在工作内存中更改了变量的值,写回主内存了,但是其他线程之前也读取了这个变量的值