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 8798857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:11:13+00:00 2026-06-14T00:11:13+00:00

On working with thread-safety, I find myself always double checking before executing code in

  • 0

On working with thread-safety, I find myself always “double checking” before executing code in a lock block and I wondered if I was doing the right thing. Consider the following three ways of doing the same thing:

Example 1:

private static SomeCollection MyCollection;
private static Object locker;
private void DoSomething(string key)
{
    if(MyCollection[key] == null)
    {
         lock(locker)
         {
              MyCollection[key] = DoSomethingExpensive(); 
         }
    }
    DoSomethingWithResult(MyCollection[key]);
}

Example 2:

private static SomeCollection MyCollection;
private static Object locker;
private void DoSomething(string key)
{
    lock(locker)
    {
         if(MyCollection[key] == null)
         {
              MyCollection[key] = DoSomethingExpensive(); 
         }
    }
    DoSomethingWithResult(MyCollection[key]);
}

Example 3:

private static SomeCollection MyCollection;
private static Object locker;
private void DoSomething(string key)
{
    if(MyCollection[key] == null)
    {
        lock(locker)
        {
             if(MyCollection[key] == null)
             {
                  MyCollection[key] = DoSomethingExpensive(); 
             }
        }
    }
    DoSomethingWithResult(MyCollection[key]);
}

I always lean towards Example 3, and here’s why I think I’m doing the right thing

  • Thread 1 enters DoSomething(string)
  • MyCollection[key] == null so Thread 1 obtains a lock, just as Thread 2 enters
  • MyCollection[key] == null is still true, so Thread 2 waits to obtain the lock
  • Thread 1 calculates the value for MyCollection[key] and adds it to the collection
  • Thread 1 releases the lock and calls DoSomethingWithResult(MyCollection[key]);
  • Thread 2 obtains the lock, by which time MyCollection[key] != null
  • Thread 2 does nothing, releases the lock and continues on its merry way

Example 1 would work, but there is a big risk that Thread 2 could redundantly calculate MyCollection[key].

Example 2 would work, but every thread would obtain a lock, even if it didn’t need to – which could be a (admittedly very small) bottleneck. Why hold up threads if you don’t need to?

Am I overthinking this and if so, what is the preferred way of handling these situations?

  • 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. Editorial Team
    Editorial Team
    2026-06-14T00:11:13+00:00Added an answer on June 14, 2026 at 12:11 am

    The first method should not be used. As you realised, it leaks, so more than one thread can end up running the expensive method. The longer that method takes, the bigger is the risk that another thread will also run it. In most cases it’s only a performance problem, but in some cases it might also be a problem that the resulting data is later on replaced by a new set of data.

    The second method is the most common way, the third method is used if the data is accessed so frequently that the locking becomes a performance issue.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working with a multi-thread program. First I redirect my stdout to a
I am working on a multi-thread program, and have a question about where to
I am working on creating a thread safe control for my windows forms application.
As I understand Working with a DataContext in Parallel is not thread-safe but only
I'm working on a Windows Service that's having some issues with Thread.Sleep() so I
I would like to separate my sigle-thread application to number of working threads. Just
I am using the thread.Abort method to kill the thread, but it not working.
I'm working on a realtime imaging system. UI thread: grabs images from a camera
The regular Thread Safety section of the MSDN documentation for StringBuilder states that: ...any
I am using some software that requires me to have thread safety disabled. I

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.