EventAggregator,它是线程安全的吗?

发布于 2024-09-01 04:36:00 字数 1128 浏览 1 评论 0原文

这是线程安全的吗?

Prism 中的 EventAggregator 是一个非常简单的类,只有一个方法。当我注意到空检查和创建新类型以添加​​到私有 _events 集合中时没有锁定,我感到很惊讶。如果两个线程同时针对同一类型调用 GetEvent(在它存在于 _events 中之前),看起来这会导致集合中出现两个条目。

    /// <summary>
    /// Gets the single instance of the event managed by this EventAggregator. Multiple calls to this method with the same <typeparamref name="TEventType"/> returns the same event instance.
    /// </summary>
    /// <typeparam name="TEventType">The type of event to get. This must inherit from <see cref="EventBase"/>.</typeparam>
    /// <returns>A singleton instance of an event object of type <typeparamref name="TEventType"/>.</returns>
    public TEventType GetEvent<TEventType>() where TEventType : EventBase
    {
        TEventType eventInstance = _events.FirstOrDefault(evt => evt.GetType() == typeof(TEventType)) as TEventType;
        if (eventInstance == null)
        {
            eventInstance = Activator.CreateInstance<TEventType>();
            _events.Add(eventInstance);
        }
        return eventInstance;
    }

Is this thread-safe?

The EventAggregator in Prism is a very simple class with only one method. I was surprised when I noticed that there was no lock around the null check and creation of a new type to add to the private _events collection. If two threads called GetEvent simultaneously for the same type (before it exists in _events) it looks like this would result in two entries in the collection.

    /// <summary>
    /// Gets the single instance of the event managed by this EventAggregator. Multiple calls to this method with the same <typeparamref name="TEventType"/> returns the same event instance.
    /// </summary>
    /// <typeparam name="TEventType">The type of event to get. This must inherit from <see cref="EventBase"/>.</typeparam>
    /// <returns>A singleton instance of an event object of type <typeparamref name="TEventType"/>.</returns>
    public TEventType GetEvent<TEventType>() where TEventType : EventBase
    {
        TEventType eventInstance = _events.FirstOrDefault(evt => evt.GetType() == typeof(TEventType)) as TEventType;
        if (eventInstance == null)
        {
            eventInstance = Activator.CreateInstance<TEventType>();
            _events.Add(eventInstance);
        }
        return eventInstance;
    }

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

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

发布评论

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

评论(3

一个人的旅程 2024-09-08 04:36:00

不,不是线程安全的。

  1. List 类本身的实例成员访问非线程安全的,根据MSDN 下的线程安全
  2. 方法本身不是线程安全的
    1. 2个线程可以同时进入该方法
    2. 两者都尝试获取 FirstOrDefault
    3. 两者都一无所获
    4. 两者都添加了新的 TEventType

我将

  1. 切换到 .NET 4 中的 System.CollectionConcurrentX 集合之一
    http://msdn.microsoft.com/en-us/库/system.collections.concurrent.aspx
    或者
  2. 自己进行锁定

No, not thread safe.

  1. The instance member access of the List class itself are NOT thread safe, as per MSDN under Thread safety
  2. The method itself is not thread safe
    1. 2 threads could enter the method at the same time
    2. Both try to get the FirstOrDefault
    3. Both get nothing
    4. Both add a new TEventType

I would

  1. switch to one of the System.CollectionConcurrentX collections in .NET 4
    http://msdn.microsoft.com/en-us/library/system.collections.concurrent.aspx
    or
  2. do your own locking
薄荷港 2024-09-08 04:36:00

这取决于“_events”是什么....

NET 4 中有一些很棒的新线程安全类...
查看 http://msdn.microsoft.com/en-我们/library/system.collections.concurrent.aspx

It depends what "_events" is...

There are some awesome new thread-safe classes in .NET 4 ...
Check out http://msdn.microsoft.com/en-us/library/system.collections.concurrent.aspx

那支青花 2024-09-08 04:36:00

好吧,根据该代码粘贴,我会说不,它不是 100% 线程安全的。

当然,你有源码,所以你自己加锁就可以了。 :)

实际上,根据一般经验,我将整个 CAL 项目包含在我的解决方案中,至少在开始时是这样。它对于调试那些奇怪的区域注册/创建异常有很大帮助......

Well, based on that code paste, I'd say no, it's not 100% thread safe.

Of course, you have the source, so you can just add the lock yourself. :)

Actually, as a general rule of thumb, I include the entire CAL project in my solution, at least at the start. It helps a lot in debugging those odd region registration/creation exceptions...

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