Пример #1
0
            public bool TryRemove(T item)
            {
                var hashcode = this._comparer.GetHashCode(item);

                while (true)
                {
                    var tables = this._tables;

                    ConcurrentHashSet <T> .GetBucketAndLockNo(hashcode, out var bucketNo, out var lockNo, tables.Buckets.Length, tables.Locks.Length);

                    lock (tables.Locks[lockNo]) {
                        // If the table just got resized, we may not be holding the right lock, and must retry.
                        // This should be a rare occurrence.
                        if (tables != this._tables)
                        {
                            continue;
                        }

                        Node?previous = null;
                        for (var current = tables.Buckets[bucketNo]; current != null; current = current.Next)
                        {
                            Debug.Assert(previous == null && current == tables.Buckets[bucketNo] || previous !.Next == current);

                            if (hashcode == current.Hashcode && this._comparer.Equals(current.Item, item))
                            {
                                if (previous == null)
                                {
                                    Volatile.Write(ref tables.Buckets[bucketNo], current.Next);
                                }
                                else
                                {
                                    previous.Next = current.Next;
                                }

                                tables.CountPerLock[lockNo]--;
                                return(true);
                            }

                            previous = current;
                        }
                    }

                    return(false);
                }
            }