/// <summary> /// Performs an atomic add into an Immutable Hash Set /// </summary> /// <param name="hashSet">The location of the hash set to add to</param> /// <param name="item">The item to add</param> /// <returns>True if the item was added, or False if it was already in the set</returns> public static bool Add <T>(ref ImmutableHashSet <T> hashSet, T item) { //**************************************** ImmutableHashSet <T> OldSet, NewSet; //**************************************** do { OldSet = hashSet; NewSet = OldSet.Add(item); if (object.ReferenceEquals(OldSet, NewSet)) { return(false); } } while (Interlocked.CompareExchange(ref hashSet, NewSet, OldSet) != OldSet); return(true); }