相关文章推荐
宽容的领带  ·  Ubuntu18.04 ...·  1 年前    · 
闷骚的钢笔  ·  Mongo学习笔记(三) ...·  2 年前    · 



The collection is threadsafe, as in "using it concurrently won't corrupt its state". It doesn't make your "contains then add" operation atomic. It's your code that isn't threadsafe




​ConcurrentBag​ ​ is threadsafe, but your code isn't:

if (!SystemNames.Contains(name))
{
SystemNames.Add(name);
}


​Contains​ ​ will execute in a thread-safe way, then ​ ​Add​ ​ will also execute in a thread-safe way, but you have no guarantee that an item haven't been added in-between.

For your needs, I recommend using a ​ ​ConcurrentDictionary​ ​ instead. Just ignore the value as you won't need it.

var SystemNames = new ConcurrentDictionary<string, bool>();


Then use the ​ ​TryAdd​ ​ method to do the "if not contains then add" in a single atomic operation:

SystemNames.TryAdd(name, true);