Volatile fields in .NET: A look inside
来源:互联网 发布:mac有没有office 编辑:程序博客网 时间:2024/05/01 10:26
http://www.codeproject.com/Articles/31283/Volatile-fields-in-NET-A-look-inside
Volatile fields in .NET: A look inside
1
2
3
4
5
Introduction
The article describes the internals of volatile fields. I've seen a lot of discussions in the web regarding volatile fields. I've performed my own small research in this area, and here are some thoughts on this.
Volatile fields and memory barrier: A look inside
The two main purposes of C# volatile fields are:
- Introduce memory barriers for all access operations to these fields. In order to improve performance, CPUs store frequently accessible objects in the CPU cache. In the case of multi-threaded applications, this can cause problems. For instance, imagine a situation when a thread is constantly reading a boolean value (read thread) and another one is responsible for updating this field (write thread). Now, if the OS will decide to run these two threads in different CPUs, it is possible that the update thread will change the value of the field on the CPU1 cache and the read thread will continue reading the value from the CPU2 cache. In other words, it will not get the change in thread1 until the CPU1 cache is invalidated. The situation can be worse if two threads update the value.
Volatile fields introduce memory barriers, which means that the CPU will always read from and write to the virtual memory, but not to the CPU cache. Nowadays, such CPU architectures as x86 and x64 have CPU cache coherency, which means that any change in the CPU cache of one processor will be propagated to other CPU caches. And, in its turn, it means that the JIT compiler for the x86 and x64 platforms makes no difference between volatile and non-volatile fields (except the one stated in item #2). Also, multi-core CPUs usually have two levels of cache: the first level is shared between CPU cores, and the second one is not. But, such CPU architectures as Itanium with a weak memory model does not have cache coherency, and therefore the
volatile
keyword and memory barriers play a significant role while designing multi-threaded applications. Therefore, I'd recommend to always usevolatile
and memory barriers even for x86 and x64 CPUs, because otherwise, you introduce CPU architecture affinity to your application.Note: you can also introduce memory barriers by using
Thread.VolatileRead
/Thread.VolatileWrite
(these two methods successfully replace thevolatile
keyword),Thread.MemoryBarrier
, or even with the C#lock
keyword etc.Below are displayed two CPU architectures: Itanium and AMD (Direct Connect architecture). As we can see, in AMD's Direct Connect architecture, all processors are connected with each other, so we have memory coherence. In the Itanium architecture, the CPUs are not connected with each other and they communicate with RAM through the System Bus.
- Prevents instruction reordering. For instance, consider we have a loop:Collapse |Copy Code
while(true){ if(myField) { //do something }}
In the case of a non-volatile field, during JIT compilation, due to performance considerations, the JIT compiler can reorder instructions in the following manner:
Collapse |Copy Codeif(myField){ while(true) { //do something }}
In case you plan to change
myField
from a separate thread, will there be a significant difference? Usually, it is recommended to use thelock
statement (Monitor.Enter
orMonitor.Exit
), but if you change only one field within this block, then the volatile field will perform significantly better than theMonitor
class.
Special thanks
I would like to thank Stefan Repas from Microsoft for providing some helpful information on this topic.
License
This article, along with any associated source code and files, is licensed underThe Code Project Open License (CPOL)
- Volatile fields in .NET: A look inside
- A look inside blocks
- A Look Inside JBoss Cache
- A quick look inside the Android emulator
- A look inside blocks: Episode 1
- A look inside blocks: Episode 1
- Volatile Fields
- Mobile Development in 2013: A Look Back
- An Introduction to TrueType Fonts: A look inside the TTF format
- 2014年12月3日,A Look Inside Presentation Controllers
- A closer look at Android ART in AndroidL
- Look into "A Neural Network in 11 lines of Python"
- How to quickly look up a word in dictionary
- [转]A Look at ASP.NET 2.0's Provider Model
- A low-level Look at the ASP.NET Architecture
- A First Look at ASP.NET v. 2.0
- A low-level Look at the ASP.NET Architecture
- A low-level Look at the ASP.NET Architecture
- 黑马程序员__ ArrayList 与HashSet 去除重复函数的 区别 equals (面试有考,重点)
- [转]Oracle字符集(客户端+服务端)的问题
- 最近比较火的框架Android Xutils 框架
- 数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历
- Android程序员必备精品资源
- Volatile fields in .NET: A look inside
- 编译内核时出现没有ncurses的错误提示
- leetcode Binary Tree Level Order Traversal II
- ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务解决
- android周边搜索 如何得到兴趣点到我的距离
- app审核客服
- 由公式抽样检查所想到的...
- HDU-#2824 The Euler function(欧拉函数+筛法)
- iOS中添加UITapGestureRecognizer手势识别后,UITableView的didSelectRowAtIndexPath失效