Thread safety in Regular expressions

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Can I have a compiled Regex object declared as a static member and call it's
match
method from multiple threads at the same time;
 
Strider said:
Can I have a compiled Regex object declared as a static member and call it's
match method from multiple threads at the same time;

Yes. From the docs:

<quote>
The Regex class is immutable (read-only) and is inherently thread safe.
Regex objects can be created on any thread and shared between threads.
</quote>

A link then says:

<quote>
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. Although many of these objects are
logically immutable, their implementations could delay computation of
some results to improve performance, and as a result, callers must
serialize access to them.

If there is a need to share Regex result objects on multiple threads,
these objects can be converted to thread-safe instances by calling
their synchronized methods. With the exception of enumerators, all
regular expression classes are thread safe or can be converted into
thread-safe objects by a synchronized method.

Enumerators are the only exception. An application must serialize calls
to collection enumerators. The rule is that if a collection can be
enumerated on more than one thread simultaneously, you should
synchronize enumerator methods on the root object of the collection
traversed by the enumerator.
</quote>
 
Thanks Jon.
Couldn't be more helpful!

Jon Skeet said:
Yes. From the docs:

<quote>
The Regex class is immutable (read-only) and is inherently thread safe.
Regex objects can be created on any thread and shared between threads.
</quote>

A link then says:

<quote>
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. Although many of these objects are
logically immutable, their implementations could delay computation of
some results to improve performance, and as a result, callers must
serialize access to them.

If there is a need to share Regex result objects on multiple threads,
these objects can be converted to thread-safe instances by calling
their synchronized methods. With the exception of enumerators, all
regular expression classes are thread safe or can be converted into
thread-safe objects by a synchronized method.

Enumerators are the only exception. An application must serialize calls
to collection enumerators. The rule is that if a collection can be
enumerated on more than one thread simultaneously, you should
synchronize enumerator methods on the root object of the collection
traversed by the enumerator.
</quote>
 
Back
Top