volatile

What is the difference between volatile & extern?

强颜欢笑 提交于 2019-12-05 10:38:04
Few days back i had an interview but, still I am searching for the answer. I would like to understand the significance of using volatile keyword. Find the code below: Two different scenario. //project1 //File1.c int abc;//Global variable /*And this variable is getting used in some other files too.*/ if(abc == 3) //Say { printf("abc == 3"); } else { printf("abc != 3"); } /*So if or else part will not be optimized because "abc" can not be predicted, the value can chage at any point of time */ //Project2 //file1.c volatile int abc;//Global variable with volatile keyword /*And this variable is

Argument of type “volatile char *” is incompatible with parameter of type “const char *”

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-05 10:31:35
I have a function whose prototype is as follows: void foo(const char * data); Elsewhere in my code, I have a global variable declared as follows volatile char var[100]; Whenever I try to do this: foo(var); The compiler throws up the following error message: Argument of type "volatile char *" is incompatible with parameter of type "const char *" Why is that the case? As I understand it, the variable in my function is not allowed to change the pointer or its contents. I understand that because my global variable is volatile, it could potentially change at any time, but seeing as it is perfectly

【java虚拟机】Java内存模型

梦想与她 提交于 2019-12-05 09:57:31
作者:平凡希 原文地址:https://www.cnblogs.com/xiaoxi/p/7518259.html 一、什么是Java内存模型 Java虚拟机规范中试图定义一种Java内存模型(Java Memory Model,JMM)来屏蔽掉各种硬件和操作系统的访问差异,以实现让Java程序在各种平台下都能达到一致的内存访问效果。 在此之前,主流程序语言(如C/C++等)直接使用物理硬件和操作系统的内存模型,因此,会由于不同平台上内存模型的差异,有可能导致程序在一套平台上并发完全正常,而在另外一套平台上并发访问却经常出错,因此在某些场景下就不许针对不同的平台来编写程序。 Java内存模型即要定义得足够严谨,才能让Java的并发内存访问操作不会产生歧义;Java内存模型也必须定义地足够宽松,才能使得虚拟机的实现有足够的自由空间去利用硬件的各种特性来获取更好的执行速度。经过长时间的验证和修补,JDK1.5(实现了JSR-133)发布之后,Java内存模型已经成熟和完善起来了,一起来看一下。 二、主内存和工作内存 Java内存模型的主要目的是定义程序中各个变量的访问规则,即在虚拟机中将变量存储到内存和从内存中取出变量这样的底层细节。注意一下,此处的变量并不包括局部变量与方法参数,因为它们是线程私有的,不会被共享,自然也不会存在竞争,此处的变量应该是实例字段

读写锁的实现

▼魔方 西西 提交于 2019-12-05 09:19:07
reference: 原子操作、内存屏障、锁 : https://www.jianshu.com/p/1d90fe6627ad 哪些操作本身是原子的? 单核处理器下中断发生在指令之间,因此单指令操作都是原子的 多核处理器下进行零次或一次对齐内存访问的汇编指令是原子的 c++版读写锁 https://blog.csdn.net/zxc024000/article/details/88814461 volatile 谈谈C/C++中的volatile: https://zhuanlan.zhihu.com/p/33074506 内存模型: https://cloud.tencent.com/developer/article/1479199 易变性。所谓的易变性,在汇编层面反映出来,就是两条语句,下一条语句不会直接使用上一条语句对应的volatile变量的寄存器内容,而是重新从内存中读取。 不可优化性。volatile告诉编译器,不要对变量进行各种激进的优化,甚至将变量直接消除,保证程序员写在代码中的指令,一定会被执行。 顺序性。能够保证Volatile变量间的顺序性,编译器不会进行乱序优化。(但CPU依然可能会乱序,x86和amd只开启了 store-load ooo ) volatile object - an object whose type is volatile

why using volatile with synchronized block?

浪尽此生 提交于 2019-12-05 08:24:36
问题 I saw some examples in java where they do synchronization on a block of code to change some variable while that variable was declared volatile originally .. I saw that in an example of singleton class where they declared the unique instance as volatile and they sychronized the block that initializes that instance ... My question is why we declare it volatile while we synch on it, why we need to do both?? isn't one of them is sufficient for the other ?? public class someClass { volatile static

different thread accessing MemoryStream

左心房为你撑大大i 提交于 2019-12-05 08:22:36
There's a bit of code which writes data to a MemoryStream object directly into it's data buffer by calling GetBuffer(). It also uses and updates the Position and SetLength() properties appropriately. This code works properly 99.9999% of the time. Literally. Only every so many 100,000's of iterations it will barf. The specific problem is that the Position property of MemoryStream suddenly returns zero instead of the appropriate value. However, code was added that checks for the 0 and throws an exception which includes log of the MemoryStream properties like Position and Length in a separate

std::atomic<int> memory_order_relaxed VS volatile sig_atomic_t in a multithreaded program

谁说我不能喝 提交于 2019-12-05 08:08:04
Does volatile sig_atomic_t give any memory order guarantees? E.g. if I need to just load/store an integer is it ok to use? E.g. here: volatile sig_atomic_t x = 0; ... void f() { std::thread t([&] {x = 1;}); while(x != 1) {/*waiting...*/} //done! } is it correct code? Are there conditions it may not work? Note: This is a over-simplifed example, i.e. I am not looking for a better solution for the given piece of code. I just want to understand what kind of behaviour I could expect from volatile sig_atomic_t in a multithreaded program according to the C++ standard. Or, if it is a case, understand

Java中的transient,volatile和strictfp,instanceof

假如想象 提交于 2019-12-05 08:06:00
不妨看看这三个词的汉语翻译:transient:短暂的;volatile:不稳定的;strictfp: FP-strict,也就是说精确浮点的意思。 下面是网上的一些解释: 1,如果用transient声明一个实例变量,当对象存储时,它的值不需要维持。 2, volatile修饰符告诉编译器被volatile修饰的变量可以被程序的其他部分改变。在多线程程序中,有时两个或更多的线程共享一个相同的实例变量。考虑效率问题,每个线程可以自己保存该共享变量的私有拷贝。实际的变量副本在不同的时候更新,如当进入synchronized方法时。 3, 用strictfp修饰类或方法,可以确保浮点运算(以及所有切断)正如早期的Java版本那样准确。切断只影响某些操作的指数。当一个类被strictfp修饰,所有的方法自动被strictfp修饰。 4,instanceof用于一种方法处理两种子类。但是推荐使用多态,这样更有优势。 来源: oschina 链接: https://my.oschina.net/u/250670/blog/58124

Android Studio 2.0 update - public static volatile com.android.tools.fd.runtime.IncrementalChange

こ雲淡風輕ζ 提交于 2019-12-05 07:59:17
After I update with Android 2.0 a new field has been added into my models object public static volatile com.android.tools.fd.runtime.IncrementalChange com.pr4.models.User.$change What problem I have faced : i.e I was reading the field using reflection package and creating a dynamic table using fields, and my app get crashed due to new field $change, If i put a check to skip particular modifier field then I have to make lot of changes. Why a new field was added at runtime in my code, how to get rid of it? Best Coder It happens due to Instant Run a new feature introduced in 2.0 Android studio

Volatile variable and non volatile reordering / visibility

偶尔善良 提交于 2019-12-05 07:35:00
问题 So I thought I knew this stuff well enough, until I read something which got me doubting my knowledge on this subject matter. I am almost certain the book is incorrect but would like to ask the community as well. PS: Have not seen the errata of the book so could well be disclosed as an error. A simplified example: public class VolatileMain { private volatile int a = 0; private String text = ""; public static void main(String[] args) throws Exception { VolatileMain vm = new VolatileMain();