Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 157299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:28:46+00:00 2026-05-11T10:28:46+00:00

Is this code correct or is there any possibility of some random threading deadlocks

  • 0

Is this code correct or is there any possibility of some random threading deadlocks etc?

Is it a good idea to use static properties and locking together? Or is static property thread-safe anyway?

Private Shared _CompiledRegExes As List(Of Regex) Private Shared Regexes() As String = {'test1.Regex', 'test2.Regex'} Private Shared RegExSetupLock As New Object  Private Shared ReadOnly Property CompiledRegExes() As List(Of Regex)     Get         If _CompiledRegExes Is Nothing Then         SyncLock RegExSetupLock              If _CompiledRegExes Is Nothing Then                 _CompiledRegExes = New List(Of Regex)(Regexes.Length - 1)                  For Each exp As String In Parser.Regexes                     _CompiledRegExes.Add(New Regex(exp, RegexOptions.Compiled Or RegexOptions.CultureInvariant Or RegexOptions.IgnoreCase))                 Next              End If          End SyncLock      End If      Return _CompiledRegExes  End Get End Property 

If it’s not obvious code is compiling regexes and storing in a List(Of Regex) so they can run faster. And it’s shared so every instance of class can get benefit out of it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T10:28:46+00:00Added an answer on May 11, 2026 at 10:28 am

    Your code isn’t thread-safe because you’re using double-checked locking without making the field volatile. Unfortunately VB.NET doesn’t have a volatile modifier, so you can’t apply the normal fix. Just acquire the lock every time instead, or use static initialization to initialize _CompiledRegExes when the type is initialized.

    See my singleton page for a general discussion of this with regards to singletons – I know this isn’t quite a singleton, but it’s close. The page gives C# code, but it should be fairly easy to understand.

    Additionally, I generally recommend making lock variables read-only. You really don’t want to be changing the value 🙂

    In general terms:

    • No, static properties aren’t thread-safe automatically
    • Yes, it’s okay to lock on a static variable (but initialize it explicitly, like you are doing)
    • Don’t try to write lock-free or low-lock code unless you really know what you’re doing. I regard myself as reasonably knowledgeable about threads, and I still don’t try using double-checked locking etc.
    • Type initialization is thread-safe (with a few caveats if you have complex initializers which end up referencing each other) so that’s a good time to do initialization like this – then you really don’t need a lock.

    EDIT: You don’t need to make the type a singleton. Just write function to initialize the list and return it, then use that function in the initializer for the variable:

    ' This has to be declared *before* _CompiledRegExes ' ' as the initializer will execute in textual order ' ' Alternatively, just create the array inside BuildRegExes ' ' and don't have it as a field at all. Unless you need the array ' ' elsewhere, that would be a better idea. ' Private Shared ReadOnly Regexes() As String = {'test1.Regex', 'test2.Regex'}  Private Shared ReadOnly _CompiledRegExes As List(Of Regex) = BuildRegExes()  Private Shared ReadOnly Property CompiledRegExes() As List(Of Regex)     Get         Return _CompiledRegExes     End Get End Property  Private Shared Function BuildRegExes() As List(Of Regex)     Dim list = New List(Of Regex)(Regexes.Length - 1)      For Each exp As String In Regexes         _CompiledRegExes.Add(New Regex(exp, RegexOptions.Compiled Or RegexOptions.CultureInvariant Or RegexOptions.IgnoreCase))     Next     Return list End Function 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 76k
  • Answers 76k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer This is probably because the IsNumeric function returns true for… May 11, 2026 at 3:12 pm
  • added an answer You won't be able to develop silverlight control projects in… May 11, 2026 at 3:12 pm
  • added an answer I'm not sure from this post whether you are implementing… May 11, 2026 at 3:12 pm

Related Questions

This was an interview question. Given Visual Studio 2008 and an icon saved as
I'm currently working on a quite big (and old, sigh) code base, recently upgraded
I'm writing a java app using eclipse which references a few external jars and
I recently inherited a small Java program that takes information from a large database,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.