Regex实例线程对于c#中的匹配是否安全

时间:2021-04-29 11:46:30

I have this regex which I am using in a Parallel.ForEach<string>. Is it safe?

我有这个regex,我在一个Parallel.ForEach 中使用它。它是安全的呢?

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 个解决方案

#1


18  

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:

Regex对象是只读的,因此是线程安全的。这是它们的返回值,匹配对象可能会引起问题。MSDN证实了这一点:

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.

Regex类本身是线程安全且不可变的(只读)。也就是说,可以在任何线程上创建Regex对象并在线程之间共享;匹配方法可以从任何线程调用,并且不会改变任何全局状态。

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

但是,Regex返回的结果对象(Match和MatchCollection)应该在单个线程上使用。

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.

我关心的是如何以一种可能并发的方式生成匹配集合,这可能导致集合行为有点怪异。有些匹配实现使用延迟求值,这可能在foreach循环中导致一些疯狂的行为。我可能会收集所有匹配项,然后在稍后对它们进行评估,以确保安全,并获得一致的性能。

#1


18  

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:

Regex对象是只读的,因此是线程安全的。这是它们的返回值,匹配对象可能会引起问题。MSDN证实了这一点:

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.

Regex类本身是线程安全且不可变的(只读)。也就是说,可以在任何线程上创建Regex对象并在线程之间共享;匹配方法可以从任何线程调用,并且不会改变任何全局状态。

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

但是,Regex返回的结果对象(Match和MatchCollection)应该在单个线程上使用。

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.

我关心的是如何以一种可能并发的方式生成匹配集合,这可能导致集合行为有点怪异。有些匹配实现使用延迟求值,这可能在foreach循环中导致一些疯狂的行为。我可能会收集所有匹配项,然后在稍后对它们进行评估,以确保安全,并获得一致的性能。