Is Regex instance thread safe for matches in C#

送分小仙女□ 提交于 2019-12-10 00:36:30

问题


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);
});

回答1:


Regex objects are read-only, and therefore are thread safe. It's their returns, the Match objects that could potentially cause problems. MSDN confirms this:

The Regex class itself is thread safe and immutable (read-only). That is, Regex objects can be created on any thread and shared between threads; matching methods can be called from any thread and never alter any global state.

However, result objects (Match and MatchCollection) returned by Regex should be used on a single thread ..

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 foreach loop. I would probably collect all the Matches and then evaluate them later, both to be safe and to get consistent performance.



来源:https://stackoverflow.com/questions/13129287/is-regex-instance-thread-safe-for-matches-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!