惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google Online Security Blog
Google Online Security Blog
博客园_首页
酷 壳 – CoolShell
酷 壳 – CoolShell
Jina AI
Jina AI
博客园 - Franky
大猫的无限游戏
大猫的无限游戏
Hugging Face - Blog
Hugging Face - Blog
博客园 - 司徒正美
V
V2EX
雷峰网
雷峰网
云风的 BLOG
云风的 BLOG
V
Visual Studio Blog
F
Full Disclosure
Y
Y Combinator Blog
V
V2EX - 技术
Attack and Defense Labs
Attack and Defense Labs
S
Security @ Cisco Blogs
Schneier on Security
Schneier on Security
Microsoft Azure Blog
Microsoft Azure Blog
SecWiki News
SecWiki News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
The GitHub Blog
The GitHub Blog
量子位
PCI Perspectives
PCI Perspectives
S
Secure Thoughts
D
Darknet – Hacking Tools, Hacker News & Cyber Security
AWS News Blog
AWS News Blog
Blog — PlanetScale
Blog — PlanetScale
爱范儿
爱范儿
K
Kaspersky official blog
B
Blog
A
Arctic Wolf
Hacker News: Ask HN
Hacker News: Ask HN
L
LangChain Blog
T
Tor Project blog
P
Privacy & Cybersecurity Law Blog
Recent Announcements
Recent Announcements
宝玉的分享
宝玉的分享
The Register - Security
The Register - Security
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
L
Lohrmann on Cybersecurity
D
Docker
A
About on SuperTechFans
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Google DeepMind News
Google DeepMind News
The Last Watchdog
The Last Watchdog
S
Security Affairs
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Privacy International News Feed
Simon Willison's Weblog
Simon Willison's Weblog

博客园 - 宝气狗

【摄影】2008年06月07日西塘 【摄影】2008年05月24日大明山 2007年11月——感受黄山,天下无山。 将基础数据类型与字节数组相互转换 托管/非托管类型对照 【数据库】遍历XML根下的一级节点 - 宝气狗 - 博客园 Enterprise Library中缓存过期策略探究 css中的nowrap 2007年9月15日 九溪-五云山-梅家坞之行 宝气狗的新闻博开始使用http://webbased.cn [设计模式] 15.Command 命令模式 [设计模式] 22.State 状态模式 [设计模式] 23.Strategy 策略模式 值类型装箱拆箱需要注意的地方 CLR如何控制类型中字段的布局 MSIL指令速查 内存一致性问题 如何循序渐进向DotNet架构师发展 关于将Queue中的数据拼接成xml的经验
内存一致性问题(续一)
宝气狗 · 2007-07-02 · via 博客园 - 宝气狗

为避免高速缓存的一致性问题,IA64架构的CPU提供了一些相关指令
volatile read 易失读取,该指令从内存读取字节,然后使高速缓存的相应字节失效
volatile write 易失写入,该指令将高速缓存的值刷新到主存中
memory fence 内存栅栏,将高速缓存的字节刷新到主存中
在System.Threading.Thread中提供了这三个方法
Thread.VolatileRead(...)
Thread.VolatileWrite(...)
Thread.MemoryBarrier();

 1internal sealed class VolatileMethod
 2{
 3    private Byte m_initialized = 0;
 4    private Int32 m_value = 0;
 5
 6    public void Thread1()
 7    {
 8        m_value = 5;
 9        Thread.VolatileWrite(ref m_initialized, 1);
10    }

11
12    public void Thread2()
13    {
14        if (Thread.VolatileRead(ref m_initialized) == 1)
15        {
16            Console.Write(m_value);
17        }

18    }

19}

在c#中,有了比较好的简化,他支持易失字段,只需要在字段前加上volatile
参见VolatileField类

 1internal sealed class VolatileField
 2{
 3    private volatile Byte m_initialized = 0;
 4    private Int32 m_value = 0;
 5
 6    public void Thread1()
 7    {
 8        m_value = 5;
 9        m_initialized = 1;
10    }

11
12    public void Thread2()
13    {
14        if (m_initialized == 1)
15        {
16            Console.Write(m_value);
17        }

18    }

19}

c#对易失操作的支持,volatile保证字段的所有读写操作实际上都是对内存的读取
其实易失操作并不是好的做法,在大部分时候,算法在访问字段时只需要很少的易失操作
比如a = a + a;//a是类中的一个字段,这里如果用易失操作,就很难让人接受
所以不论是Thread的VolatileRead和VolatileWrite,还是c#的volatile关键词,都不建议使用
而推荐使用互锁方法或更高层次的线程同步构造