简体   繁体   English

多个线程从Lookup读取是否安全 <TKey, TElement> ?

[英]Is it safe for multiple threads to read from a Lookup<TKey, TElement>?

Is it safe for multiple threads to read from a Lookup<TKey, TElement> ? 多个线程从Lookup<TKey, TElement>读取是否安全?

Lookup<TKey, TElement> is immutable, however MSDN states: Lookup<TKey, TElement>是不可变的,但MSDN声明:

Any public static (Shared in Visual Basic) members of this type are thread safe. 此类型的任何公共静态(在Visual Basic中为Shared)成员都是线程安全的。 Any instance members are not guaranteed to be thread safe. 任何实例成员都不保证是线程安全的。

Though I shudder to imagine it, I'm wondering if the machine that pumps out MSDN documentation could be incorrect. 虽然我不禁想象它,但我想知道抽出MSDN文档的机器是否可能不正确。

因为我不喜欢冒险,我可能不得不在1年后调试一个模糊的多线程相关错误,我将假设在没有手动同步的情况下使用这个类是不安全的。

As long as there is no writing, doing just reading is thread-safe. 只要没有写作,只做读取是线程安全的。 This is valid in any case. 这在任何情况下都是有效的。

Your question is in a sense orthogonal to the notion of thread-safety. 你的问题在某种意义上与线程安全的概念正交。 A write in combination with a write or read is not thread-safe, but multiple reads without writing are thread-safe. 与写入或读取组合的写入不是线程安全的,但是没有写入的多次读取是线程安全的。

What MSDN says about instance members not being guaranteed to be thread-safe can only be valid in case of non-thread-safe scenarios, which by definition imply a write operation. MSDN所说的实例成员不能保证是线程安全的,只有在非线程安全的情况下才有效,根据定义,这意味着写操作。

This is standard disclaimer for all most classes as you've probably noticed. 这是您可能已经注意到的 所有 大多数课程的标准免责声明。 Some methods may be thread safe, but "are not guaranteed". 某些方法可能是线程安全的,但“不保证”。

Generally it is safe to read from collection using multiple threads if there are no writers to collection. 通常,如果没有集合的编写者,使用多个线程从集合中读取是安全的。 If you need to update collection at the same time - use appropriate synchronization or built in thread safe collections like SynchronizedKeyedCollection . 如果需要同时更新集合 - 使用适当的同步或内置的线程安全集合,如SynchronizedKeyedCollection

Because the Lookup<TKey,TElement> is immutable, means that you will get the same values for all members. 因为Lookup<TKey,TElement>是不可变的,意味着您将获得所有成员的相同值。 It does not mean that the items stored in it cannot be modified. 这并不意味着无法修改存储在其中的项目。 So the collection is indeed not thread safe. 所以这个集合确实不是线程安全的。 A perfect example would be that most linq is lazy evaluated, and creating the enumerator could involve executing the lazy code. 一个完美的例子是大多数linq是惰性求值的,创建枚举器可能涉及执行惰性代码。 Trying to enumerate in two separate threads could cause the collection to be realized twice producing the wrong result. 试图在两个单独的线程中枚举可能导致集合被实现两次产生错误的结果。

Update: Now that the source code is available on https://referencesource.microsoft.com it is confirmed that internal state is set during method calls without regard to multithreading meaning that you could have race conditions and the Lookup<TKey,TElement> class is in fact not thread safe. 更新:既然源代码在https://referencesource.microsoft.com上可用,则确认在方法调用期间设置了内部状态,而不考虑多线程意味着您可能具有竞争条件和Lookup<TKey,TElement>类实际上不是线程安全的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Lookup 的意义是什么<tkey, telement> ?</tkey,> - What is the point of Lookup<TKey, TElement>? Linq GroupBy和Lookup <TKey, TElement> 。项目 - Linq GroupBy and Lookup<TKey, TElement>.Item 将Lookup <TKey,TElement>转换为其他数据结构c# - Converting Lookup<TKey, TElement> into other data structures c# 如何转换查找的元素;即ILookup <TKey,Derived>到ILookup <TKey,Base>? - How to cast TElement of lookup; i.e. ILookup<TKey, Derived> to ILookup<TKey, Base>? 分组 <TKey,TElement> 合计 - IGrouping<TKey,TElement> Totals 如何接口 IGrouping<out tkey, out telement> 产生多个值?</out> - How can interface IGrouping<out TKey, out TElement> yield multiple values? 不应该ILookup <TKey, TElement> 是(声明)TElement中的协变? - Shouldn't ILookup<TKey, TElement> be (declared) covariant in TElement? 是否可以在 IEnumerable 中返回将 TElement 与 TKey 匹配的匿名类型<IGrouping<TKey, TElement> &gt; GroupBy() 返回? - Is it possible to return an anonymous type that matches TElement to TKey in IEnumerable<IGrouping<TKey, TElement>> returned by GroupBy()? static 变量是否是线程安全的,因为您可以从多个线程读取/写入而不会崩溃? - Are static variables thread safe in the sense that you can read/write from multiple threads without it crashing? 从多个线程更新 IProgress 是否安全? - Is it safe to update IProgress from multiple threads?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM