Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / October 2006

Tip: Looking for answers? Try searching our database.

Are Regex objects thread-safe?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
mnowosad - 26 Oct 2006 01:18 GMT
The MSDN documentation about the Regex class seems to have contradictory
information. At the "Remarks" section, the documentation states:

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

However, on the "Thread Safety" section of the same class, the documentation
states:

"Any public static (Shared in Visual Basic) members of this type are thread
safe. Any instance members are not guaranteed to be thread safe.".

So, for instance, if i have an instance of Regex and the only method I will
ever call on it is the instance method "IsMatch()", to validate text input,
can I share this instance across different threads? Can multiple threads
concurrently call "IsMatch()" method on this Regex instance without any major
problem? Or do I need to serialize the calls to "IsMatch()" method in this
case, using the "lock" statement?
Carl Daniel [VC++ MVP] - 26 Oct 2006 02:07 GMT
> The MSDN documentation about the Regex class seems to have
> contradictory information. At the "Remarks" section, the
[quoted text clipped - 10 lines]
> thread safe. Any instance members are not guaranteed to be thread
> safe.".

This is boilerplate text that doesn't apply to Regex and should be removed
from this doc page.

> So, for instance, if i have an instance of Regex and the only method
> I will ever call on it is the instance method "IsMatch()", to
> validate text input, can I share this instance across different
> threads?

Yes.

> Can multiple threads concurrently call "IsMatch()" method on
> this Regex instance without any major problem?

Yes.

> Or do I need to
> serialize the calls to "IsMatch()" method in this case, using the
> "lock" statement?

No, no need for anything like that.

-cd
mnowosad - 26 Oct 2006 08:46 GMT
Hi Carl,

Thanks for the helpful answer. Yes, Microsoft should rephrase the standard
"Thread Safety" section for classes like Regex. It is confusing and
misleading.

Marcos

> > The MSDN documentation about the Regex class seems to have
> > contradictory information. At the "Remarks" section, the
[quoted text clipped - 33 lines]
>
> -cd

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.