热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在HashSet<T>中包含线程安全

如何解决《在HashSet<T>中包含线程安全》经验,为你挑选了1个好方法。

看一下.NET源代码Contains中的HashSet类代码,我找不到任何Contains线程安全的原因?

HashSet提前加载了一个值,然后检查Contains多线程.AsParallel()环.

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



1> xanatos..:

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

本身不能成为项目线程安全的(但有HashSet这个问题应尽量减少,因为你不能从中提取项目.仍然是GetHashCode()Equals()必须是线程安全的.如果,例如,他们访问所加载懒对象按需,它们可能不是线程安全的,或者它们可能缓存/记忆一些数据以加速后续操作)

您必须确保在最后一次写入之后有一个Thread.MemoryBarrier()(在与写入相同的线程中完成)或等效的,否则在另一个线程上读取可能会读取不完整的数据

你必须确保在每个线程(不同于你写的那个)中,在第一次读取之前有一个Thread.MemoryBarrier().请注意,如果HashSet在创建/启动其他线程之前已经"准备好"(最后使用Thread.MemoryBarrier()),那么Thread.MemoryBarrier()就没有必要了,因为线程不能过时读取内存(因为他们不存在).各种操作导致隐含Thread.MemoryBarrier().例如,如果在HashSet填充之前创建的线程,输入a Wait()并且un-WaitedHashSet填充之后(加上它Thread.MemoryBarrier()),则退出a Wait()会导致隐式Thread.MemoryBarrier()

使用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;
    }
}


推荐阅读
author-avatar
jack1881的天空_925
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有