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

  • Home
  • SEARCH
  • 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 8873081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:22:34+00:00 2026-06-14T18:22:34+00:00

The idea is to get a value (data) from a variable if doesn’t exist,

  • 0

The idea is to get a value (“data”) from

  • a variable
  • if doesn’t exist, then from a file
  • if doesn’t exist, then from a server
public static string LoadData(int id)
{
    if (_isDataLoaded[id - 1])
    {
      return _data[id - 1];
    }

    if (File.Exists(_fileName))
    {
        bool dataExists;
        string cachedData;
        if (GetCachedConstant(id, out cachedData)) //read from a file
        {
            lock (_locker)
            {
                _isDataLoaded[id - 1] = true;
                _data[id - 1] = cachedData;
            }

            return _data[id - 1];
        }
    }

    string remoteData = GetFromServer(id);
    lock (_locker)
    {
        CacheToFile(id, remoteData); //write to a file
        _data[id - 1] = remoteData;
        _isDataLoaded[id - 1] = true;
    }

    return _data[id - 1];
}

This code is used by many threads. Although it seems to be thread safe, actually it isn’t. I tested it, thus, that gives me that idea and makes me sure. There should be double check of existing _data before writing. For example, it should be like double check used in pattern Singleton usually.

Someone please let me understand how do I implement it here?

EDIT:

string[] _data;
bool[] _isDataLoaded.

EDIT2:

The code above might be used with .NET < 4.0, so it’s not allowed to use Lasy there. The only question I have for now, is should I use volatile in case I would use double-checked locking?

volatile string[] _data;
volatile bool[] _isDataLoaded.
  • 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-14T18:22:35+00:00Added an answer on June 14, 2026 at 6:22 pm

    I can see two obvious sources of threading problems

    • What type is _data and _isDataLoaded? If these types are not thread safe then neither is your code arrays are only thread safe if no two threads access the same element, which could happen in the above code. Try using a ConcurrentDictionary instead.
    • Depending on the exact implementation of GetCachedConstant and CacheToFile there is a potential race condition whereby thread A could begin writing cached data to a file causing thread B to go down the File.Exists route when in fact the file only contains partially written data

    I’d guess the culprit is the first of these two – there might be other problems but unless these types are thread safe no amount of double checked locking will save you unless you also synchronize access to those objects.

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

Sidebar

Related Questions

How can I seek this sb string variable to get those variables:IMKB's value: 64882,72
I am trying to get some master data from server in JSON format and
I created a (very simple) class wich gets a string value from an intent.
I'm having multiple sheets in an excel file sheet1 contains some data from A1
currently I am using ListActivity. I use a cursor to get the data from
When I attempt to run the following test in IntelliJ IDEA I get the
does someone have an idea how to get the environment variables on Google-AppEngine ?
It seems that I get the idea of call stack in programming language design.
The idea here is to get better programmers right out of college. I think
any idea how i can get the code below to produce this output? 1

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.