Multithreading improvements in .NET 4

前端 未结 3 1977
天涯浪人
天涯浪人 2020-12-23 02:24

I have heard that the .NET 4 team has added new classes in the framework that make working with threads better and easier.

Basically the question is what are the new

3条回答
  •  天命终不由人
    2020-12-23 02:53

    Strictly speaking this is C# 4.0 and not a new class, but events now have a smarter form of locking which if I've understood the change correctly, removes the need for reems of locking code as shown below (taken from this article by Jon Skeet):

    SomeEventHandler someEvent;
    readonly object someEventLock = new object();
    
    public event SomeEventHandler SomeEvent
    {
        add
        {
            lock (someEventLock)
            {
                someEvent += value;
            }
        }
        remove
        {
            lock (someEventLock)
            {
                someEvent -= value;
            }
        }
    }
    

提交回复
热议问题