不安全:storeFence() & loadFence() 与 易失性
我正在检查关于java栅栏的这篇很棒的文章,其中栅栏用于以下示例,以确保并发线程可以始终读取从其他线程更新的最新值:
// CPU 0:
void shutDownWithFailure(void)
{
failure = 1; // must use SOB as this is owned by CPU 1
SFENCE // next instruction will execute after all SOBs are processed
shutdown = 1; // can execute immediately as it is owned be CPU 0
}
// CPU1:
void workLoop(void)
{
while (shutdown == 0) { ... }
LFENCE // next instruction will execute after all LOBs are processed
if (failure) { ...}
}
我的问题是,针对易失性
使用栅栏有什么好处。
以上面的例子为例,如果我同时进行 failure
和 shutdown
volatile ,它应该达到相同的效果吗?
I'm checking this great post on java fences, in which fences are used for following example, to ensure that concurrent thread can always read latest value updated from other threads:
// CPU 0:
void shutDownWithFailure(void)
{
failure = 1; // must use SOB as this is owned by CPU 1
SFENCE // next instruction will execute after all SOBs are processed
shutdown = 1; // can execute immediately as it is owned be CPU 0
}
// CPU1:
void workLoop(void)
{
while (shutdown == 0) { ... }
LFENCE // next instruction will execute after all LOBs are processed
if (failure) { ...}
}
My question is, what's the advantage of using fences against volatile
.
Taking above example, if I make both failure
and shutdown
volatile, it should achieve the same?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想说,这一切都在与该问题链接的 JEP 171 中进行了描述,在顶部的“动机”部分。总而言之:它的目的是提供一种记录的方式来访问这些机制,并为并发访问提供类的进一步实现和扩展。
并不意味着使用它们总是比
易失性
更有优势。I would say, it's all described in the JEP 171 that is linked to that question, in the "Motivation" section at the top. To sum it up: it is meant to provide a documented way to access these mechanisms and to provide further implementations and extensions of the classes for concurrent access.
Doesn't mean to use them is always an advantage over
volatile
.