volatile

Usage of volatile specifier in C/C++/Java

…衆ロ難τιáo~ 提交于 2019-11-30 15:47:04
问题 While going through many resources on multithreaded programming, reference to volatile specifier usually comes up. It is clear that usage of this keyword is not a reliable way to achieve synchronization between multiple threads atleast in C/C++ and Java (versions 1.4 and earlier). Here is what wikipedia lists (without explaining how) as typical usages this specifier:- allow access to memory mapped devices allow uses of variables between setjmp and longjmp allow uses of variables in signal

Volatile variable explanation in Java docs

坚强是说给别人听的谎言 提交于 2019-11-30 15:44:07
问题 when a thread reads a volatile variable, it sees not just the latest change to the volatile, but also the side effects of the code that led up the change This is mentioned at http://docs.oracle.com/javase/tutorial/essential/concurrency/atomic.html Can someone please provide an example of this? This first gave me an impression that the thread that reads a volatile variable will synchronize with the writer thread and wait until the write is done. But that clearly is not the case. An example

Memory Model: preventing store-release and load-acquire reordering

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-30 15:25:12
It is known that, unlike Java's volatiles, .NET's ones allow reordering of volatile writes with the following volatile reads from another location. When it is a problem MemoryBarier is recommended to be placed between them, or Interlocked.Exchange can be used instead of volatile write. It works but MemoryBarier could be a performance killer when used in highly optimized lock-free code. I thought about it a bit and came with an idea. I want somebody to tell me if I took the right way. So, the idea is the following: We want to prevent reordering between these two accesses: volatile1 write

[转]c语言volatile 关键字

不问归期 提交于 2019-11-30 14:32:16
转自 https://blog.csdn.net/weibo1230123/article/details/81984805 volatile是用于编译器的优化,对硬件上的memory order无用! 1、 编译器优化 介绍: 由于内存访问速度远不及CPU处理速度 ,为提高机器整体性能,在硬件上引入硬件高速缓存Cache,加速对内存的访问。另外在现代CPU中指令的执行并不一定严格按照顺序执行,没有相关性的指令可以乱序执行,以充分利用CPU的指令流水线,提高执行速度。以上是硬件级别的优化。再看软件一级的优化:一种是在编写代码时由程序员优化,另一种是由编译器进行优化。 编译器优化常用的方法有:将内存变量缓存到寄存器;调整指令顺序充分利用CPU指令流水线,常见的是重新排序读写指令。 对常规内存进行优化的时候,这些优化是透明的,而且效率很好。由编译器优化或者硬件重新排序引起的问题的解决办法是在从硬件(或者其他处理器)的角度看必须以特定顺序执行的操作之间设置内存屏障(memory barrier),Linux 提供了一个宏解决编译器的执行顺序问题。 void Barrier(void) 这个函数通知编译器插入一个内存屏障, 但对硬件无效 ,编译后的代码会把当前CPU寄存器中的所有修改过的数值存入内存,需要这些数据的时候再重新从内存中读出。 2、volatile总是与优化有关,

Should my Scala actors' properties be marked @volatile?

左心房为你撑大大i 提交于 2019-11-30 13:56:20
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. You don't need to mark them as volatile. The execution of your code isn't inside a synchronized block, but the actor will always pass through one before

Why aren't variables in Java volatile by default?

断了今生、忘了曾经 提交于 2019-11-30 13:15:44
问题 Possibly similar question: Do you ever use the volatile keyword in Java? Today I was debugging my game; It had a very difficult threading problem that would show up every few minutes, but was difficult to reproduce. So first I added the synchronized keyword to each of my methods. That didn't work. Then I added the volatile keyword to every field. The problem seemed to just fix itself. After some experimentation I found that the field responsible was a GameState object which kept track of my

Volatile and Thread.MemoryBarrier in C#

最后都变了- 提交于 2019-11-30 13:01:52
To implement a lock free code for multithreading application I used volatile variables, Theoretically : The volatile keyword is simply used to make sure that all threads see the most updated value of a volatile variable; so if thread A updates the variable value and thread B read that variable just after that update is happened it will see the most updated value that written recently from thread A. As I read in a C# 4.0 in a Nutshell book that this is incorrect because applying volatile doesn’t prevent a write followed by a read from being swapped. Could this problem being solved by putting

WebRTC-线程模型(1)

心不动则不痛 提交于 2019-11-30 12:25:17
在介绍WebRTC的线程模型之前,先介绍webrtc线程模型中用到的几个简单、常用的模块或函数。webrtc是一个代码宝库,且它本身跨平台(windows,linux,ios,android),不管是哪个平台上面开发,都可以从中学习到很多有用的东西。 一、 设置线程名 platform_thread.cc void SetCurrentThreadName( const char * name) { #if defined(WEBRTC_WIN) struct { DWORD dwType; LPCSTR szName; DWORD dwThreadID; DWORD dwFlags; } threadname_info = { 0x1000 , name, static_cast <DWORD>(- 1 ), 0 }; __try { ::RaiseException( 0x406D1388 , 0 , sizeof (threadname_info) / sizeof (DWORD), reinterpret_cast <ULONG_PTR*>(&threadname_info)); } __except (EXCEPTION_EXECUTE_HANDLER) { } #elif defined(WEBRTC_LINUX) || defined(WEBRTC_ANDROID)

Java volatile关键字-禁用CPU缓存

Deadly 提交于 2019-11-30 12:06:14
可见性: 一个县城对共享变量的修改,另一个线程能够立刻看到。 volatile的意义时禁用CPU缓存 public class VolatileExample { int x = 0; volatile boolean v = false; public void writer(){ x = 42; v = true; } public void reader(){ if (v){ System.out.println(x); } } 假设线程A执行writer()方法,按照 volatile语义,会把变量 “v=true” 写入内存;假 设线程B执行reader()方法,同样按照 volatile 语义,线程B会从内存中读取变量v,如果线程B看到 “v == true” 时,那么线程B看到的变量x是多少呢? 1.5以前的版本会出现x=0的情况因为变量x可能被CPU缓存而导致 可见性问题。这个问题在1.5版本已经被圆满解决了。x=42,为什么呢? 如果线程B读到了“v=true”,那么线程A设置的“x=42”对线程B是可见的。也就是说,线程B能看到 “x == 42” ,有没有一种恍然大悟的感觉?这就是1.5版本对volatile语义的增强,这个增强意义重大,1.5版本 的并发工具包(java.util.concurrent)就是靠volatile语义来搞定可见性的。 ****

volatile能否保住线程安全性

余生颓废 提交于 2019-11-30 11:57:29
volatile能否保住线程安全性? 今天有人问 volatile能否保住线程安全性问题。那答案是不能。 那咱们分析下 为什么不能保证安全性问题。 一、线程安全问题发生在读写方面,原子性操作可以保证线程安全。 二、volatile 关键字的作用是 1、可见性 2、禁止指令重排续。 volatile 只是保证了数据的可见性 没有保证数据的一致性。 在并发的情况下 一样会产生线程安全问题。 例如 两个线程同时取到 volatile修饰的变量a= 1 对该变量做++操作a++。 因为++操作不是原子性操作,在一个线程刚执行++操作时,cpu时间片切换到另一个线程上 那线程读取的数还是1,那还是进行++操作。这样就会产生多线程问题。 来源: https://blog.csdn.net/weixin_43992555/article/details/101346550