c# - 为什么在 C# 中将委托(delegate)分配给本地副本线程安全,任何代码都可以证明这一点?

标签 c# delegates thread-safety

有人告诉我 C# 中的多播委托(delegate)是不可变的。但下面的代码似乎证明它不是。

Action a = () => { };
Action b = a;
Console.WriteLine(ReferenceEquals(a, b));

它将显示 True 而不是 False。这是另一个例子:

public delegate void MyHandler();
public class Klass
{
    public event MyHandler onCreate;

    public void Create()
    {
        MyHandler handler = onCreate;
        if (handler != null)
        {
            handler();
        }

        Console.WriteLine(ReferenceEquals(handler, onCreate));
    }
}

该类将按如下方式调用:

Klass k = new Klass();
k.onCreate += () => { };

k.Create();

同上它会显示True,那么如果本地副本与原始副本引用相同,它怎么会是线程安全的呢?

最佳答案

How could it be thread safe if the local copy is the same reference as the orignal one?

我认为您可能误解了不变性的含义。维基百科是这样定义的:

In object-oriented and functional programming, an immutable object is an object whose state cannot be modified after it is created. This is in contrast to a mutable object, which can be modified after it is created.

从变量ab的引用分配不会改变委托(delegate)的状态。因此,调用同一个委托(delegate)的多个线程保证调用同一个构造的委托(delegate)。请注意,这并不意味着该委托(delegate)引用的底层方法是线程安全的。请记住,C# 中的委托(delegate)是引用类型,它们是按值复制的,在这种情况下,值是 GC 堆中保存的对象的地址,而不是值本身的副本。有关委托(delegate)线程安全的更多信息,请参阅 Are C# delegates thread-safe?

关于c# - 为什么在 C# 中将委托(delegate)分配给本地副本线程安全,任何代码都可以证明这一点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29529796/

相关文章:

c++ - 如果一个线程正在执行的仿函数被移动会发生什么?

c++ - 使用 C++ Builder 在 Windows 上截取屏幕截图的线程安全

c# - 通过本地主机服务器在 asp.net 中发送电子邮件

c# - Linq ASP.NET 中的正则表达式

ios - 如果 View 不在 View 层次结构中,则不调用 GADBannerView 委托(delegate)方法

kotlin - 如何将依赖项注入(inject) Kotlin 中的接口(interface)委托(delegate)?

Java并发对象池?

c# - 如何使用 Windows SDK 更新 "Additional Clocks"设置

c# - 绑定(bind)在没有 INotifyPropertyChanged 的​​情况下工作,为什么?

ios - swift:如何在另一个 View Controller 中访问一个变量?