本文介绍了避免比赛条件?算子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

?。可用于调用委托或事件的运算符会避免出现竞争情况吗?

Does the ?. operator that can be used to invoke a delegate or event avoid race conditions?

例如。手动避免竞争条件:

Eg. avoid race-condition manually:

//The event-invoking method that derived classes can override.
        protected virtual void OnShapeChanged(ShapeEventArgs e)
        {
            // Make a temporary copy of the event to avoid possibility of
            // a race condition if the last subscriber unsubscribes
            // immediately after the null check and before the event is raised.
            EventHandler<ShapeEventArgs> handler = ShapeChanged;
            if (handler != null)
            {
                handler(this, e);
            }
        }

来源:

推荐答案

...

新方法是线程安全的,因为编译器生成代码以
一次评估PropertyChanged

The new way is thread-safe because the compiler generates code toevaluate PropertyChanged one time only

这篇关于避免比赛条件?算子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 17:12