C# 中的 Volatile 和 Thread.MemoryBarrier

发布于 2024-10-04 09:45:34 字数 1249 浏览 10 评论 0原文

为了实现多线程应用程序的无锁代码,我使用了易失性变量, 理论上易失性关键字只是用来确保所有线程都能看到易失性变量的最新值;因此,如果线程 A 更新变量值,并且线程 B 在更新发生后立即读取该变量,它将看到最近从线程 A 写入的最新值。 正如我在C# 4.0 in a Nutshell一书中读到的那样 这是不正确因为

应用易失性并不会阻止先写后读的交换。

是否可以通过在每次获取 volatile 变量之前放置 Thread.MemoryBarrier() 来解决此问题,例如:

private volatile bool _foo = false;

private void A()
{
    //…
    Thread.MemoryBarrier();
    if (_foo)
    {
        //do somthing
    }
}

private void B()
{
    //…
    _foo = true;
    //…
}

如果这解决了问题;考虑我们有一个 while 循环,它依赖于其条件之一的该值;将 Thread.MemoryBarrier() 放在 while 循环之前是解决问题的正确方法吗?例如:

private void A()
{
    Thread.MemoryBarrier();
    while (_someOtherConditions && _foo)
    {
        // do somthing.
    }
}

为了更准确,我希望 _foo 变量在任何线程随时请求时提供最新的值;因此,如果在调用变量之前插入 Thread.MemoryBarrier() 可以解决问题,那么我可以使用 Foo 属性而不是 _foo 并执行 Thread.MemoryBarrier() 在该属性的获取中,例如:

Foo
{
    get 
    {
        Thread.MemoryBarrier();
        return _foo;
    }
    set
    {
        _foo = value;
    }
}

To implement a lock free code for multithreading application I used volatile variables,
Theoretically: The volatile keyword is simply used to make sure that all threads see the most updated value of a volatile variable; so if thread A updates the variable value and thread B read that variable just after that update is happened it will see the most updated value that written recently from thread A.
As I read in a C# 4.0 in a Nutshell book that
this is incorrect because

applying volatile doesn’t prevent a write followed by a read from being swapped.

Could this problem being solved by putting Thread.MemoryBarrier() before every get of the volatile variable like:

private volatile bool _foo = false;

private void A()
{
    //…
    Thread.MemoryBarrier();
    if (_foo)
    {
        //do somthing
    }
}

private void B()
{
    //…
    _foo = true;
    //…
}

And if this solves the problem; consider we have a while loop that depend on that value at one of its conditions; is putting Thread.MemoryBarrier() before the while loop is a correct way to fix the issue? example:

private void A()
{
    Thread.MemoryBarrier();
    while (_someOtherConditions && _foo)
    {
        // do somthing.
    }
}

To be more accurate I want the _foo variable to give its most fresh value when any thread asking for it at any time; so if inserting Thread.MemoryBarrier() before calling the variable fixes the issue then could I use Foo property instead of _foo and do a Thread.MemoryBarrier() within the get of that property Like:

Foo
{
    get 
    {
        Thread.MemoryBarrier();
        return _foo;
    }
    set
    {
        _foo = value;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

辞别 2024-10-11 09:45:34

“C# In a Nutshell”是正确的,但它的说法没有实际意义。为什么?

  • 一个“读”(没有“易失性”),无论如何都会保证按程序顺序发生如果它影响单个线程中的逻辑
  • 一个“写”后跟 在您的示例中担心多线程程序完全没有意义

让我们澄清一下。拿你的原始代码来说:

private void A() 
{ 
    //… 
    if (_foo) 
    { 
        //do something 
    } 
}

如果线程调度程序已经检查了 _foo 变量,但它在 //do some 注释之前被挂起,会发生什么?好吧,此时您的其他线程可能会更改 _foo 的值,这意味着您所有的 volatile 和 Thread.MemoryBarriers 都毫无意义!如果 _foo 的值为 false 时绝对必须避免 do_something,那么您别无选择,只能使用锁。

但是,如果当 _foo 突然变为 false 时,可以执行 do some ,那么这意味着 volatile 关键字足以满足您的需求。

需要明确的是:所有告诉您使用记忆屏障的响应者都是不正确的或提供了过度杀伤力。

The "C# In a Nutshell" is correct, but its statement is moot. Why?

  • A 'write' followed by a 'read', withot 'volatile', is guaranteed to occur in program-order anyway if it effects logic within a single thread
  • The 'write' before a 'read' in a multi-threaded program is utterly pointless to worry about in your example.

Let's clarify. Take your original code:

private void A() 
{ 
    //… 
    if (_foo) 
    { 
        //do something 
    } 
}

What happens if the thread scheduler has already checked the _foo variable, but it gets suspended just before the //do something comment? Well, at that point your other thread could change the value of _foo, which means that all your volatiles and Thread.MemoryBarriers counted for nothing!!! If it is absolutely essential that the do_something be avoided if the value of _foo is false, then you have no choice but to use a lock.

However, if it is ok for the do something to be executing when suddenly _foo becomes false, then it means the volatile keyword was more than enough for your needs.

To be clear: all the responders who are telling you to use a memory barrier are incorrect or are providing overkill.

子栖 2024-10-11 09:45:34

这本书正确
CLR 的内存模型表明加载和存储操作可以重新排序。这适用于易失性非易失性变量。

将变量声明为易失性仅意味着加载操作将具有获取语义,而存储操作将具有释放语义。此外,编译器将避免执行某些优化,这些优化依赖于以序列化、单线程方式访问变量的事实(例如,将加载/存储提升到循环之外)。

单独使用 volatile 关键字不会创建临界区,也不会导致线程神奇地相互同步。

当您编写无锁代码时,您应该极其小心。这并不简单,即使是专家也很难做到正确。
无论您试图解决的原始问题是什么,很可能有一种更合理的方法来解决它。

The book is correct.
The CLR's memory model indicates that load and store operations may be reordered. This goes for volatile and non-volatile variables.

Declaring a variable as volatile only means that load operations will have acquire semantics, and store operations will have release semantics. Also, the compiler will avoid performing certain optimizations that relay on the fact that the variable is accessed in a serialized, single-threaded fashion (e.g. hoisting load/stores out of loops).

Using the volatile keyword alone doesn't create critical sections, and it doesn't cause threads to magically synchronize with each other.

You should be extremely careful when you write lock free code. There's nothing simple about it, and even the experts have trouble to get it right.
Whatever is the original problem you're trying to solve, it's likely that there's a much more reasonable way to do it.

别再吹冷风 2024-10-11 09:45:34

在第二个示例中,您还需要在循环内放置一个 Thread.MemoryBarrier(); ,以确保每次检查循环条件时都能获得最新值。

In your second example, you would need to also put a Thread.MemoryBarrier(); inside the loop, to make sure you get the most recent value every time you check the loop condition.

情栀口红 2024-10-11 09:45:34

此处提取...

class Foo
{
  int _answer;
  bool _complete;

  void A()
  {
    _answer = 123;
    Thread.MemoryBarrier();    // Barrier 1
    _complete = true;
    Thread.MemoryBarrier();    // Barrier 2
  }

  void B()
  {
    Thread.MemoryBarrier();    // Barrier 3
    if (_complete)
    {
      Thread.MemoryBarrier();       // Barrier 4
      Console.WriteLine (_answer);
    }
  }
}

障碍 1 和 4 阻止了此示例
从写“0”开始。障碍 2 和 3
提供新鲜度保证:他们
确保如果 B 在 A 之后跑,则读取
_complete 将评估为 true。

因此,如果我们回到您的循环示例...它应该是这样的...

private void A()
{
    Thread.MemoryBarrier();
    while (_someOtherConditions && _foo)
    {
        //do somthing
        Thread.MemoryBarrier();
    }
}

Pulled from here...

class Foo
{
  int _answer;
  bool _complete;

  void A()
  {
    _answer = 123;
    Thread.MemoryBarrier();    // Barrier 1
    _complete = true;
    Thread.MemoryBarrier();    // Barrier 2
  }

  void B()
  {
    Thread.MemoryBarrier();    // Barrier 3
    if (_complete)
    {
      Thread.MemoryBarrier();       // Barrier 4
      Console.WriteLine (_answer);
    }
  }
}

Barriers 1 and 4 prevent this example
from writing “0”. Barriers 2 and 3
provide a freshness guarantee: they
ensure that if B ran after A, reading
_complete would evaluate to true.

So if we go back to your looping example...this is how it should look...

private void A()
{
    Thread.MemoryBarrier();
    while (_someOtherConditions && _foo)
    {
        //do somthing
        Thread.MemoryBarrier();
    }
}
不乱于心 2024-10-11 09:45:34

微软自己关于内存屏障的说法是:

仅在具有弱内存排序的多处理器系统(例如,采用多个 Intel Itanium 处理器的系统)上才需要 MemoryBarrier。

对于大多数用途,C# lock 语句、Visual Basic SyncLock 语句或 Monitor 类提供了更简单的数据同步方法。

Microsoft's own words on memory barriers:

MemoryBarrier is required only on multiprocessor systems with weak memory ordering (for example, a system employing multiple Intel Itanium processors).

For most purposes, the C# lock statement, the Visual Basic SyncLock statement, or the Monitor class provide easier ways to synchronize data.

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