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

推荐订阅源

WordPress大学
WordPress大学
The GitHub Blog
The GitHub Blog
F
Fortinet All Blogs
Cloudbric
Cloudbric
P
Palo Alto Networks Blog
T
Threatpost
T
Tor Project blog
T
Tenable Blog
AWS News Blog
AWS News Blog
Project Zero
Project Zero
L
LangChain Blog
Cyberwarzone
Cyberwarzone
Engineering at Meta
Engineering at Meta
雷峰网
雷峰网
C
CERT Recently Published Vulnerability Notes
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Security Latest
Security Latest
云风的 BLOG
云风的 BLOG
I
Intezer
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
A
Arctic Wolf
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
V
Vulnerabilities – Threatpost
C
Cybersecurity and Infrastructure Security Agency CISA
MongoDB | Blog
MongoDB | Blog
aimingoo的专栏
aimingoo的专栏
K
Kaspersky official blog
Jina AI
Jina AI
N
News | PayPal Newsroom
T
The Blog of Author Tim Ferriss
D
DataBreaches.Net
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
Recorded Future
Recorded Future
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Secure Thoughts
TaoSecurity Blog
TaoSecurity Blog
P
Privacy & Cybersecurity Law Blog
P
Proofpoint News Feed
MyScale Blog
MyScale Blog
IT之家
IT之家
Forbes - Security
Forbes - Security
The Hacker News
The Hacker News
Last Week in AI
Last Week in AI
T
Threat Research - Cisco Blogs
Y
Y Combinator Blog

博客园 - 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 + 不加锁访问对象造成的错误。