避免比赛条件?算子 [英] Avoid race condition ?. operator

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

问题描述

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

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);
            }
        }

来源: msdn

推荐答案


空条件成员访问的另一种用法是以更少的代码以线程安全的方式调用委托

Another use for the null-condition member access is invoking delegates in a thread-safe way with much less code.

...

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

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

MSDN来源

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆