volatile

C# volatile variable: Memory fences VS. caching

ぃ、小莉子 提交于 2019-12-03 13:34:32
问题 So I researched the topic for quite some time now, and I think I understand the most important concepts like the release and acquire memory fences . However, I haven't found a satisfactory explanation for the relation between volatile and the caching of the main memory. So, I understand that every read and write to/from a volatile field enforces strict ordering of the read as well as the write operations that precede and follow it (read-acquire and write-release). But that only guarantees the

java--volatile关键字

此生再无相见时 提交于 2019-12-03 13:10:51
转:https://www.cnblogs.com/selene/p/5972882.html volatile不能保证数据同步 volatile关键字比较少用,原因无外乎两点,一是在Java1.5之前该关键字在不同的操作系统上有不同的表现,所带来的问题就是移植性较差;而且比较难设计,而且误用较多,这也导致它的"名誉" 受损。   我们知道,每个线程都运行在栈内存中,每个线程都有自己的工作内存(Working Memory,比如寄存器Register、高速缓冲存储器Cache等),线程的计算一般是通过工作内存进行交互的,其示意图如下图所示:      从示意图上我们可以看到,线程在初始化时从主内存中加载所需的变量值到工作内存中,然后在线程运行时,如果是读取,则直接从工作内存中读取,若是写入则先写到工作内存中,之后刷新到主内存中,这是JVM的一个简答的内存模型,但是这样的结构在多线程的情况下有可能会出现问题,比如:A线程修改变量的值,也刷新到了主内存,但B、C线程在此时间内读取的还是本线程的工作内存,也就是说它们读取的不是最"新鲜"的值,此时就出现了不同线程持有的公共资源不同步的情况。   对于此类问题有很多解决办法,比如使用synchronized同步代码块,或者使用Lock锁来解决该问题,不过,Java可以使用volatile更简单地解决此类问题

How do I know if gcc agrees that something is volatile?

*爱你&永不变心* 提交于 2019-12-03 12:52:27
Consider the following: volatile uint32_t i; How do I know if gcc did or did not treat i as volatile? It would be declared as such because no nearby code is going to modify it, and modification of it is likely due to some interrupt. I am not the world's worst assembly programmer, but I play one on TV. Can someone help me to understand how it would differ? If you take the following stupid code: #include <stdio.h> #include <inttypes.h> volatile uint32_t i; int main(void) { if (i == 64738) return 0; else return 1; } Compile it to object format and disassemble it via objdump, then do the same

What is meant by “fast-path” uncontended synchronization?

前提是你 提交于 2019-12-03 11:56:11
From the Performance and Scalability chapter of the JCIP book : The synchronized mechanism is optimized for the uncontended case(volatile is always uncontended), and at this writing, the performance cost of a "fast-path" uncontended synchronization ranges from 20 to 250 clock cycles for most systems. What does the author mean by fast-path uncontended synchronization here? I'm not familiar with the topic of the book, but in general a “fast path” is a specific possible control flow branch which is significantly more efficient than the others and therefore preferred, but cannot handle complex

why can't a local variable be volatile in C#?

僤鯓⒐⒋嵵緔 提交于 2019-12-03 11:23:20
问题 public void MyTest() { bool eventFinished = false; myEventRaiser.OnEvent += delegate { doStuff(); eventFinished = true; }; myEventRaiser.RaiseEventInSeperateThread() while(!eventFinished) Thread.Sleep(1); Assert.That(stuff); } Why can't eventFinished be volatile and does it matter? It would seem to me that in this case the compiler or runtime could become to smart for its own good and 'know' in the while loop that eventFinished can only be false. Especially when you consider the way a lifted

What does—or did—“volatile void function( … )” do?

两盒软妹~` 提交于 2019-12-03 10:47:05
I've seen How many usage does "volatile" keyword have in C++ function, from grammar perspective? about use of the volatile keyword on functions, but there was no clear explanation of what Case 1 from that question did. Only a statement by one of the respondents that it seemed pointless/useless. Yet I cannot quite accept that statement, since the AES software implementations for GNUC have been used for literally years, and they have a number of functions like this: INLINE volatile void functionname( /* ... */ ) { /* ... */ asm( /* ... */ ) // embedded assembly statements /* ... */ } There has

Volatile function

自作多情 提交于 2019-12-03 10:23:58
Summary : What does the keyword volatile do when applied to a function declaration in C and in C++? Details : I see that it's possible to compile a function that is marked as volatile . However, I'm not sure what compiler optimization (if any) this prevents. For instance I created the following test case: volatile int foo() { return 1; } int main() { int total = 0; int i = 0; for(i = 0; i < 100; i++) { total += foo(); } return total; } When I compile with clang -emit-llvm -S -O3 test.c (gcc would also work but the llvm IR is more readable in my opinion) I get: target triple = "x86_64-unknown

Using inline assembly with serialization instructions

匿名 (未验证) 提交于 2019-12-03 09:52:54
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: We consider that we are using GCC (or GCC -compatible) compiler on a X86_64 architecture, and that eax , ebx , ecx , edx and level are variables ( unsigned int or unsigned int* ) for input and output of the instruction (like here ). asm("CPUID":::); asm volatile("CPUID":::); asm volatile("CPUID":::"memory"); asm volatile("CPUID":"=a"(eax),"=b"(ebx),"=c"(ecx),"=d"(edx)::"memory"); asm volatile("CPUID":"=a"(eax):"0"(level):"memory"); asm volatile("CPUID"::"a"(level):"memory"); // Not sure of this syntax asm volatile("CPUID":"=a"(eax),"=b"(ebx)

volatile简介与原理

耗尽温柔 提交于 2019-12-03 09:47:24
一、计算机内存模型 1. CPU的高速缓存: a. 由于CPU的速度远远大于IO速度和主存速度,所以CPU加了一层高速缓存,把主存的数据加载到高速缓存 b. CPU高速缓存为某个CPU独有,只与运行在该CPU的线程有关 2. 缓存一致性问题: a. 当一个在主存里的变量被多个线程访问,成为共享变量,每个线程会把共享变量拷贝到当前线程的高速缓存,操作完后更新回主存 b. 可能一个线程操作的变量,被别的线程已经更新了,但是当前线程不知道 二、Java内存模型JMM 1、Java内存模型规定所有的变量都是存在主存当中(类似于前面说的物理内存),每个线程都有自己的工作内存(类似于前面的高速缓存) 2. 线程对变量的所有操作都必须在工作内存中进行,而不能直接对主存进行操作 3. 并且每个线程不能访问其他线程的工作内存 三、并发编程 1. 要想并发程序正确地执行,必须要保证原子性、可见性以及有序性,只要有一个没有被保证,就有可能会导致程序运行不正确 2. 原子性:一个操作要么不执行,要么全执行 3. 可见性:当一个线程修改了共享变量,其他线程立即可见 4. 有序性:代码执行是有先后顺序的,JVM会进行指令重排,指令重排不会影响单线程的操作结果,但有可能影响多线程的结果 四、volatile关键字 1. volatile关键字轻量级的synchronized,保证多线程正确执行 2. 可见性:

volatile struct = struct not possible, why?

淺唱寂寞╮ 提交于 2019-12-03 09:38:05
struct FOO{ int a; int b; int c; }; volatile struct FOO foo; int main(void) { foo.a = 10; foo.b = 10; foo.c = 10; struct FOO test = foo; return 0; } This won't compile, because struct FOO test = foo; generates an error: error: binding reference of type 'const FOO&' to 'volatile FOO' discards qualifiers How can I copy a volatile struct into another struct in C++ (before C++11)? Many people suggested to just delelte volatile, but I can't do that in that case, because I want to copy the current SPI-Reg setttings inside a µC and this is declared volatile by the manufacturer headers. I want to copy