不安全:storeFence() & loadFence() 与 易失性

发布于 2025-01-17 05:05:36 字数 735 浏览 5 评论 0原文

我正在检查关于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) { ...}
}

我的问题是,针对易失性使用栅栏有什么好处。
以上面的例子为例,如果我同时进行 failureshutdown 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假情假意假温柔 2025-01-24 05:05:36

我想说,这一切都在与该问题链接的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文