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

推荐订阅源

Attack and Defense Labs
Attack and Defense Labs
T
Threatpost
C
Cybersecurity and Infrastructure Security Agency CISA
H
Hackread – Cybersecurity News, Data Breaches, AI and More
I
Intezer
C
Cyber Attacks, Cyber Crime and Cyber Security
The Register - Security
The Register - Security
量子位
Security Latest
Security Latest
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
MyScale Blog
MyScale Blog
J
Java Code Geeks
Apple Machine Learning Research
Apple Machine Learning Research
Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
Spread Privacy
Spread Privacy
Jina AI
Jina AI
博客园 - 【当耐特】
P
Palo Alto Networks Blog
Last Week in AI
Last Week in AI
SecWiki News
SecWiki News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
G
GRAHAM CLULEY
宝玉的分享
宝玉的分享
Hacker News - Newest:
Hacker News - Newest: "LLM"
T
The Blog of Author Tim Ferriss
V
Vulnerabilities – Threatpost
有赞技术团队
有赞技术团队
T
Tor Project blog
H
Hacker News: Front Page
A
Arctic Wolf
NISL@THU
NISL@THU
A
About on SuperTechFans
云风的 BLOG
云风的 BLOG
Engineering at Meta
Engineering at Meta
V
V2EX
N
News and Events Feed by Topic
Webroot Blog
Webroot Blog
Know Your Adversary
Know Your Adversary
P
Privacy International News Feed
I
InfoQ
D
Docker
L
LINUX DO - 最新话题
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
U
Unit 42

博客园 - John Rambo

定时关机脚本 Mean shift算法处理图像(Scilab & C#) Arimoto–Blahut algorithm (Mathematica) 1分钟搞定QuickSort算法 C#行列式计算程序 MIDL语法详解 (译) Virtual PC2007 + Redhat9下的网络配置 关于IFromatterProvider Total KMP 分析函数调用的汇编指令 why c++/cli 对透明代理源码的一些理解 ReaderWriterLockSlim CallContext类 GDI+ is F**king unbelievable A better timer 第一个Postsharp插件 WeakReference System. ThreadStaticAttribute
About double-checked locking
John Rambo · 2008-11-27 · via 博客园 - John Rambo

知道double-checked locking的时候应该使用volatile,但是没有仔细想过为什么。今天想起这个问题,费解了一上午,现在终于想到一个解释。

//broken double-checked locking

Code

这个例子在ia64上会有问题。因为ia64支持写reorder,因此Singleton构造函数中对message字段的赋值有可能被调整到lock范围中对instance静态字段的赋值之后。因此可能存在这样一个时刻,instance已经保存了一个Singleton实例的引用。但是这个引用的message字段还保持为null。当然这个时刻必然是处于lock的范围内,因为Monitor.Exit具有释放语义,在其他线程Enter之前,instance静态字段和message实例字段就都已经被更新到内存当中。

我今天上午一直费解的是,既然这个不一致状态是在lock之内,为什么会对其他线程有影响?难道会有sb不加锁访问instance么?刚才忽然抬头看到了double-checked 中lock之前的check...

在第一个线程处于lock中的时候,有可能内存中的instance静态字段已经被赋值,但是内存中的message实例还没有被赋值。这个时候第二个线程粉墨登场,虽然第一个线程还持有锁,但由于double-checked中的第一个check不加锁,因此看到instance已经初始化好了,于是就获得了单例。然后第二个线程访问message字段的时候发现居然是null耶,what the hell...

我想这个就是ia64中double-checked locking is broken的原因。一个reorder + 不加锁访问对象造成的错误。