Csharp/C#教程:在HashSet 中包含线程安全分享


在HashSet 中包含线程安全

查看.NET源代码中HashSet类中Contains的代码,我找不到Contains不是线程安全的原因吗?

我正在提前使用值加载HashSet ,然后在multithreading中检查ContainsAsParallel()循环。

这有什么理由不安全吗? 当我实际上不需要存储值时,我不愿意使用ConcurrentDictionary

通常通常 )仅用于读取的集合是“非正式”线程安全的(.NET中没有我知道在读取期间自行修改的集合)。 有一些警告:

使用memoization / lazy loading /任何你想要调用它的类的简单示例可以打破线程的安全性。

 public class MyClass { private long value2; public int Value1 { get; set; } // Value2 is lazily loaded in a very primitive // way (note that Lazy *can* be used thread-safely!) public long Value2 { get { if (value2 == 0) { // value2 is a long. If the .NET is running at 32 bits, // the assignment of a long (64 bits) isn't atomic :) value2 = LoadFromServer(); // If thread1 checks and see value2 == 0 and loads it, // and then begin writing value2 = (value), but after // writing the first 32 bits of value2 we have that // thread2 reads value2, then thread2 will read an // "incomplete" data. If this "incomplete" data is == 0 // then a second LoadFromServer() will be done. If the // operation was repeatable then there won't be any // problem (other than time wasted). But if the // operation isn't repeatable, or if the incomplete // data that is read is != 0, then there will be a // problem (for example an exception if the operation // wasn't repeatable, or different data if the operation // wasn't deterministic, or incomplete data if the read // was != 0) } return value2; } } private long LoadFromServer() { // This is a slow operation that justifies a lazy property return 1; } public override int GetHashCode() { // The GetHashCode doesn't use Value2, because it // wants to be fast return Value1; } public override bool Equals(object obj) { MyClass obj2 = obj as MyClass; if (obj2 == null) { return false; } // The equality operator uses Value2, because it // wants to be correct. // Note that probably the HashSet doesn't need to // use the Equals method on Add, if there are no // other objects with the same GetHashCode // (and surely, if the HashSet is empty and you Add a // single object, that object won't be compared with // anything, because there isn't anything to compare // it with! :-) ) // Clearly the Equals is used by the Contains method // of the HashSet return Value1 == obj2.Value1 && Value2 == obj2.Value2; } } 

假设您提前使用值加载了集合,则可以使用System.Collections.Immutable库中的ImmutableHashSet 。 不可变集合将自己宣传为线程安全 ,因此我们不必担心HashSet的“非官方”线程安全性。

 var builder = ImmutableHashSet.CreateBuilder(); // The builder is not thread safe builder.Add("value1"); builder.Add("value2"); ImmutableHashSet set = builder.ToImmutable(); 

上述就是C#学习教程:在HashSet 中包含线程安全分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

 if (set.Contains("value1")) // Thread safe operation { ... } 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/985405.html

(0)
上一篇 2021年12月21日
下一篇 2021年12月21日

精彩推荐