I have this regex which I am using in a Parallel.ForEach<string>. Is it safe?
Regex reg = new Regex(SomeRegexStringWith2Groups);
Parallel.ForEach<string>(MyStrings.ToArray(), (str) =>
{
foreach (Match match in reg.Matches(str)) //is this safe?
lock (dict) if (!dict.ContainsKey(match.Groups[1].Value))
dict.Add(match.Groups[1].Value, match.Groups[2].Value);
});
Regexobjects are read-only, and therefore are thread safe. It’s their returns, theMatchobjects that could potentially cause problems. MSDN confirms this:I’d be concerned about how your Match collection is being generated in a way that might be concurrent, which could cause the collection to act kinda weird. Some Match implementations use delayed evaluation, which could cause some crazy behavior in that
foreachloop. I would probably collect all the Matches and then evaluate them later, both to be safe and to get consistent performance.