volatile

Auto optimisation for L cache for object's variables?

穿精又带淫゛_ 提交于 2019-12-10 19:49:44
问题 Frankly, this is a continue of this my question, inspired by this answer: https://stackoverflow.com/a/53262717/1479414 Let's suppose we have a class: public class Foo { private Integer x; public void setX(Integer x) { this.x = x; } public Integer getX() { return this.x; } } And let us consider a very specific scenario , when we have just two threads which interact with the x variable: At time 1, a thread T1 is created At time 2, T1 sets the value: foo.setX(123); At time 3, a thread T2 is

Assignment to volatile variable in C#

ⅰ亾dé卋堺 提交于 2019-12-10 18:19:50
问题 My understanding of C# says (thanks to Jeff Richter & Jon Skeet) that assignment is "atomic". What is not is when we mix reads & writes (increment/decrement) and hence we need to use methods on the Interlocked. If have only Read & assign would both the operations be atomic? public class Xyz { private volatile int _lastValue; private IList<int> AvailableValues { get; set; } private object syncRoot = new object(); private Random random = new Random(); //Accessible by multiple threads public int

Fastest way to safely read contents of long[] whose elements are changed concurrently

為{幸葍}努か 提交于 2019-12-10 17:44:35
问题 When you have a long[] myArray = new long[256]; whose items are changed by multiple threads using Interlocked.Increment(ref myArray[x]) it sure won't be possible to get a snapshot of myArray at one point in time, as there are non-locked writes going on concurrently, so I'm not trying to get that. So do I really have to Volatile.Read of every single element like this to get a copy of all values at some point in the past? long[] copy = new long[256]; for (int i = 0; i < 256; i++) copy[i] =

Java - use of volatile only makes sense in multiprocessor systems?

两盒软妹~` 提交于 2019-12-10 17:39:57
问题 Use of volatile only makes sense in multiprocessor systems. is this wrong? i'm trying to learn about thread programming, so if you know any good articles/pdfs ... i like stuff that mentions a bit about how the operating system works as well not just the language's syntax. 回答1: volatile is used to ensure all thread see the same copy of the data. If there is only one thread reading/writing to a field, it doesn't need to be volatile. It will work just fine, just be a bit slower. In Java you don

并发编程(五)线程同步

我是研究僧i 提交于 2019-12-10 17:28:25
一、 引言   多线程的开发过程中,也许会遇到这么一个场景:多个线程同时操作一个变量时,线程之间会有时间差,而在时间差内,该共享数据的值也许已经发生了改变,那么我们要怎么才能保证在多线程的环境下,每个线程读取到的数据值都是最新的呢?线程同步机制了解一下~ 二、 线程同步的“锁”   前面了解了多线程场景下,需要保证每个线程读取数据的值都要是最新的,那么我们就需要一个“锁”,来保证当前线程操作此数据时,别的线程无法使用,相当于当前线程“锁”住了此时数据的读写权限,下面我们来学习下如何实现这一场景。   首先我们先了解几大神器,以及其大致的作用: synchronized : 关键字 ,可以修饰方法,也可以修饰代码块 volatile : 特殊域变量 ReentrantLock : 重入锁 Atomic : 原子变量 三、 线程同步详解 synchronized同步方法    定义: 有synchronized关键字修饰的方法。 每个java对象都有一个 内置锁 ,使用synchronized关键字修饰方法时, 内置锁就会“锁住”整个方法。 每个线程在调用该方法前,都需要获得内置锁,否则就处于阻塞状态。 PS: 若用synchronized关键字修饰 静态方法 ,此时如果调用该静态方法,将 会 锁住整个类 /** * synchronized同步方法 */ public class

java并发源码:volatile

删除回忆录丶 提交于 2019-12-10 17:07:27
java并发源码:volatile volatile的定义: ​ java编程语言运行线程访问共享变量,为了确保共享变量能被准确和一致地更新,线程应该确保通过排它锁单独获得这个变量。如果一个变量被声明成volatile,java线程内存模型确保所有线程看到这个变量的值是一致的。(可见性) volatile的作用: 1.保证变量在多线程中的可见性 ​ 为了提高处理速度,处理器不直接和内存进行通信,而是先将内存中的数据读到工作内存中后再进行操作,但操作完不知道何时会回写到内存中。 ​ 如果对有volatile声明的变量进行写操作,JVM会向处理器发送Lock前缀指令,将这个变量的值,回写到主内存中。 ​ 为了保证其它线程中的缓存一致,每个处理器经过嗅探在总线上传播的数据来检查自己缓存的值是否过期,如果发现自己缓存行对应的内存地址被修改,就会将当前处理器的缓存行置为无效。当处理器再次进行修改操作时,会重新从主内存中读取数据。 实现原则: ​ 1.Lock前缀指令会引起处理器缓存回写到内存中。 ​ 2.一个处理器的缓存回写到内存中会导致其它处理器的缓存无效。 public class VolatileTest { static volatile int x = 0 ; public static void increase ( ) { x ++ ; } public static void

type_info doesn't account for cv qualifiers: is this right?

梦想的初衷 提交于 2019-12-10 16:20:14
问题 Is it the correct behaviour or is it a quirk of g++4.5 that this code prints 1? #include <iostream> #include <typeinfo> using namespace std; int main(){ struct A{}; cout<<(typeid(A)==typeid(const A)&&typeid(A)==typeid(const volatile A)&&typeid(A)==typeid(volatile A)); } I thought that types differing for cv-qualifiers were threated as very distinct types, even though less cv-qualified types could be implicitly cast to more cv-qualified types. 回答1: typeid ignores cv qualifiers, as per the C++

What is the correct way of using C++ objects (and volatile) inside interrupt routines?

给你一囗甜甜゛ 提交于 2019-12-10 15:59:14
问题 I am currently working with Atmel AVR microcontrollers (gcc), but would like the answer to apply to the microcontroller world in general, i.e. usually single-threaded but with interrupts. I know how to use volatile in C code when accessing a variable that can be modified in an ISR. For example: uint8_t g_pushIndex = 0; volatile uint8_t g_popIndex = 0; uint8_t g_values[QUEUE_SIZE]; void waitForEmptyQueue() { bool isQueueEmpty = false; while (!isQueueEmpty) { // Disable interrupts to ensure

Volatile local variable in LazyInitializer.EnsureInitialized?

情到浓时终转凉″ 提交于 2019-12-10 15:56:43
问题 I was looking at LazyInitializer.EnsureInitialized(ref T, Func{T}) in Reflector, and there appears to be a volatile local variable in that method volatile object local1 = s_barrier; . I can think of two possible reasons for this: .NET may get to use features that are not supported by a given language, or The actual code does not declare a volatile local variable, but when the compiled code is decompiled by Reflector, it looks like a volatile local variable. Does anyone know which is the case

Java's volatile in C?

送分小仙女□ 提交于 2019-12-10 15:51:37
问题 I'm aware of the use of volatile in Java. That is (based on the wikipedia article): There is a global ordering on the reads and writes to a volatile variable. This implies that every thread accessing a volatile field will read its current value before continuing, instead of (potentially) using a cached value. I also I'm aware that there exists the volatile keyword in C but in a quite different context, mainly to be used in memory-mapped I/O. So I was wondering, is there some construct like