volatile

Is the 'volatile' keyword still broken in C#?

社会主义新天地 提交于 2019-12-03 08:14:49
问题 Joe Albahari has a great series on multithreading that's a must read and should be known by heart for anyone doing C# multithreading. In part 4 however he mentions the problems with volatile: Notice that applying volatile doesn’t prevent a write followed by a read from being swapped, and this can create brainteasers. Joe Duffy illustrates the problem well with the following example: if Test1 and Test2 run simultaneously on different threads, it’s possible for a and b to both end up with a

U-BOOT1.2.0移植S3C2440成功

孤街醉人 提交于 2019-12-03 07:52:13
主要参考 http://blog.chinaunix.net/u1/34474/showart.php?id=397315 文章,tekkamna写的很详细,我就直接搬过来,记录如下:(本人在他的基础上修改了DM9000x的驱动,SDRAM的刷新参数,和网络控制芯片的基地址)交叉编译器用的是3.3.2版本。 首先,U-Boot1.2.0还没有支持s3c2440,这次移植是用2410的文件稍作修改而成的。其实2440和2410的区别主要是2440的主频更高,增加了摄像头接口和AC‘97音频接口;寄存器方面,除了新增模块的寄存器外, 移植所要注意的是NAND FlASH控制器的寄存器有较大的变化、芯片的时钟频率控制寄存器(芯片PLL的寄存器)有一定的变化。 其他寄存器 基本 是兼容的。 一、 在U-Boot中建立自己的开发板类型,并测试编译。 我为开发板取名叫: hongge2440 0 在工作目录下解压U-Boot。 $tar zxvf u-boot.git.tar.gz 1 进入U-Boot目录,修改Makefile $cd u-boot.git/ $vi Makefile #为hongge2440建立编译项 sbc2410x_config: unconfig @$(MKCONFIG) $(@:_config=) arm arm920t sbc2410x NULL s3c24x0

volatile variables and memory barrier in java

泄露秘密 提交于 2019-12-03 07:46:20
问题 I've got a data structure which consists of linked nodes. You can think of it as of a simple LinkedList. Each node of the list consists of some value and a next field pointing the other node or null if it is the last node. The first node works as a root, it has no value it only points to the next node. All the other nodes are practically immutable that is once they are created neither their value nor their next field change during lifetime, unless the structure is being disposed which relates

Does Monitor.Wait ensure that fields are re-read?

删除回忆录丶 提交于 2019-12-03 07:43:54
问题 It is generally accepted (I believe!) that a lock will force any values from fields to be reloaded (essentially acting as a memory-barrier or fence - my terminology in this area gets a bit loose, I'm afraid), with the consequence that fields that are only ever accessed inside a lock do not themselves need to be volatile . (If I'm wrong already, just say!) A good comment was raised here, questioning whether the same is true if code does a Wait() - i.e. once it has been Pulse() d, will it

The volatile key word and memory consistency errors

狂风中的少年 提交于 2019-12-03 07:31:38
In the oracle Java documentation located here , the following is said: Atomic actions cannot be interleaved, so they can be used without fear of thread interference. However, this does not eliminate all need to synchronize atomic actions, because memory consistency errors are still possible. Using volatile variables reduces the risk of memory consistency errors, because any write to a volatile variable establishes a happens-before relationship with subsequent reads of that same variable. This means that changes to a volatile variable are always visible to other threads. What's more, it also

“pseudo-atomic” operations in C++

坚强是说给别人听的谎言 提交于 2019-12-03 07:26:24
问题 So I'm aware that nothing is atomic in C++. But I'm trying to figure out if there are any "pseudo-atomic" assumptions I can make. The reason is that I want to avoid using mutexes in some simple situations where I only need very weak guarantees. 1) Suppose I have globally defined volatile bool b, which initially I set true. Then I launch a thread which executes a loop while(b) doSomething(); Meanwhile, in another thread, I execute b=true. Can I assume that the first thread will continue to

Read and Write atomic operation implementation in the Linux Kernel

≯℡__Kan透↙ 提交于 2019-12-03 07:12:18
问题 Recently I've peeked into the Linux kernel implementation of an atomic read and write and a few questions came up. First the relevant code from the ia64 architecture: typedef struct { int counter; } atomic_t; #define atomic_read(v) (*(volatile int *)&(v)->counter) #define atomic64_read(v) (*(volatile long *)&(v)->counter) #define atomic_set(v,i) (((v)->counter) = (i)) #define atomic64_set(v,i) (((v)->counter) = (i)) For both read and write operations, it seems that the direct approach was

java并发学习-第九章 指令重排序

流过昼夜 提交于 2019-12-03 07:11:37
一、happns-before    happns-before是学习指令重排序前的一个必须了解的知识点,他的作用主要是就是用来判断代码的执行顺序。   1.定义    happens-before是用来指定两个操作之间的执行顺序。提供跨线程的内存可见性。   在java内存模型中,如果一个操作执行的结果需要对另一个操作可见,那么这两个操作之间必然存在happens-before关系   2.happens-before规则    a.程序顺序规则    单线程中的每个操作,总是前一个操作happens-before于该线程中的任意后续操作。   简单的说,这个规则就是代码按照顺序执行。    b.监视器规则    对一个锁的解锁,总是happens-before于随后对这个锁的加锁。   这句话可以理解成对于同一把锁,释放与获取是线程可见的;释放锁的操作总是happens-before于获取锁的操作 。    c.volatile变量规则    对一个volatile域的写总是happens-before于任意后续对这个volatile域的读。   就是说被volatile修饰的变量,在线程中是是可见的。    d.传递性    这个规则与程序性规则相识:有A、B、C三变量,如果根据程序性规则:     A变量 happens-before B变量,     B变量

Why Double checked locking is 25% faster in Joshua Bloch Effective Java Example

自作多情 提交于 2019-12-03 06:36:41
Hi below is the snippet from Effective Java 2nd Edition. Here the author claims the following piece of code is 25% faster more than in which u do not use result variable. According to the book "What this variable does is to ensure that field is read only once in the common case where it’s already initialized." . I am not able to understand why this code would be fast after the value is initialized as compare to of if we do not use the Local variable result. In either case you will have only one volatile read after initialization whether you use the local variable result or not. // Double-check

when should a member function be both const and volatile together?

一个人想着一个人 提交于 2019-12-03 06:20:36
I was reading about volatile member function and came across an affirmation that member function can be both const and volatile together . I didn't get the real use of such a thing. Can anyone please share their experience on practical usage of having member function as const and volatile together. I wrote small class to test the same: class Temp { public: Temp(int x) : X(x) { } int getX() const volatile { return X; } int getBiggerX() { return X + 10; } private: int X; }; void test( const volatile Temp& aTemp) { int x = aTemp.getX(); } int main(int argc, char* argv[]) { const volatile Temp