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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:04:14+00:00 2026-05-13T12:04:14+00:00

I have some read-only data that I want to initialise and then re-initialise periodically

  • 0

I have some read-only data that I want to initialise and then re-initialise periodically in a thread-safe manner. For initialisation I’ve pulled in Joe Duffy’s LazyInit and LazyInitOnceOnly structs as detailed in his blog, which use the Double-Checked locking pattern. So my current implementation of the getter simply wraps around his LazyInitOnceOnly.Value property, with added space given to a time out check:

So the code is as follows:

public class MyData {
  public DateTime TimeStamp { get; set; }
  //actual shared data ommitted

  public MyData() { TimeStamp = DateTime.Now; }
}

public SharedDataContainer
{
  //data to be initialised thread-safe, and shared.
  //assume delegate passed on construction simply 'new's the object,
  private LazyInitOnceOnly<MyData> _sharedDataInit;
  //receives the result from the _sharedDataInit.Value property
  private MyData _sharedData;
  //time-out and reinitialise after 24 hours
  private TimeSpan _timeOut = new TimeSpan(24,0,0);

  public MyData SharedData
  {
    get{
      //slight adaptation of the use of the LazyInitOnceOnly struct - 
      //because we want to replace _sharedData later after an expiry time out.
      if(_sharedData == null)
        _sharedData = _sharedDataInit.Value;
      //need best ideas for this bit:
      if((DateTime.Now - _sharedData.TimeStamp) > _timeOut)
      {
        ReInitialise();
      }
      return _sharedData;
    }
  }
}

When the data is identified as out of date, the old data should be returned, but the new data should be prepared on a separate thread and swapped in when ready – so as not to block the caller. All subsequent reads from the data should return the old value until it’s updated.

So I considered queueing new thread like this in the ReInitialise() method:

() => {
  //assume constructor pulls in all the data and sets timestamp
  _sharedData = new MyData();
}

The _sharedData overwrite in the thread will occur atomically, so that’s fine. But with this code, until the rebuild is complete, all subsequent reads will try and trigger a threaded rebuild – since they are reading the old _sharedData’s TimeStamp property.

What’s the best way to ensure that only one rebuild is triggered?

  • 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-05-13T12:04:15+00:00Added an answer on May 13, 2026 at 12:04 pm

    Alternatively, (again not using the LazyInit stuff) set up an Int32 m_buildState = 0 in the constructor. Set a m_publishData member (in this approach this is your custom data object type not a LazyInit object type) to null.

    In the getter, set d = Interlocked.CompareExchange(ref m_buildState, 1, 0). Here d is a local decision variable.

    If d==2 check to see whether the data update timeout has occurred; if so, next test if Interlocked.CompareExchange(ref m_buildState, 3, 2)==2. If this is true, start a background thread to rebuild the data. Return m_publishData. (The last steps of the background rebuilding thread must be first to update m_publishData, then second to set m_buildState to 2.)

    If d==3 return the m_publishData member.

    If d==1 wait for d>=2. To do this optimally, wait for an event to occur (you could spin waiting/testing for d>=2 for a bit first if you want to optimise the code). Then return m_publishData.

    If d==0, do a rebuild on the current thread, then set m_publishData to the data object and then set m_buildState to 2 then signal event.

    I am assuming here that the time taken by the rebuild thread to rebuild is not long enough to necessitate another rebuild and that timeouts on concurrency operations are not needed. If these are not safe assumption, some more checks will be needed.

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

Sidebar

Related Questions

I think I read somewhere that some modules only have object oriented interfaces (
I have a part of data that only should be read once. I have
Noticed that if I want to read some data and if I do not
I have some data that I want to store somewhere in my Rails app
I am building application that required some data from iPhone's Call log(read only). The
I have some status data that I want to cache from a database. Any
In a nutshell: I have some database table data that I want to populate
I have a web page with a read-only text box which shows some HTML
I have some 2TB read only (no writing once created) files on a RAID
I have a Core Data-based iPhone app with a pre-populated read-only database. What protection

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.