ConcurrentBag of strings and using .Contains in Parallel.ForEach
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);