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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:39:56+00:00 2026-05-26T18:39:56+00:00

that’s what I have: a Windows Service C# multithreaded the service uses a Read-Write-Lock

  • 0

that’s what I have:

  • a Windows Service
    • C#
    • multithreaded
    • the service uses a Read-Write-Lock (multiple reads at one time, writing blocks other reading/writing threads)
  • a simple, self-written DB
    • C++
    • small enough to fit into memory
    • big enough not wanting to load it on startup (e.g. 10GB)
    • read-performance is very important
    • writing is less important
    • tree structure
    • informations held in tree nodes are stored in files
    • for faster performance, the files are loaded only the first time they are used and cached
    • lazy initialization for faster DB startup

As the DB will access those node informations very often (in the magnitude of several thousand times a second) and as I don’t write very often, I’d like to use some kind of double checked locking pattern.

I know there is many questions about the double checked locking pattern here, but there seems to be so many different opinions, so I don’t know what’s the best for my case. What would you do with my setup?

Here’s an example:

  • a tree with 1 million nodes
  • every node stores a list of key-value-pairs (stored in a file for persistence, file size magnitude: 10kB)
  • when accessing a node for the first time, the list is loaded and stored in a map (sth. like std::map)
  • the next time this node is accessed, I don’t have to load the file again, I just get it from the map.
  • only problem: two threads are simultaneously accessing the node for the first time and want to
    write to the cache-map. This is very unlikely to happen, but it is not impossible. That’s where I need thread-safety, which should not take too much time, as I usually don’t need it (especially, once the whole DB is in memory).
  • 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-26T18:39:57+00:00Added an answer on May 26, 2026 at 6:39 pm

    About double checked locking:

    class Foo
    {
      Resource * resource;
    
      Foo() : resource(nullptr) { }
    public:
      Resource & GetResource()
      {
        if(resource == nullptr)
        {
          scoped_lock lock(mutex); 
          if(resource == nullptr)
            resource = new Resource();
        }
        return *resource;
      }
    }
    

    It is not thread-safe as you check whether the address of resource is null. Because there is a chance that resource pointer is assigned to a non-null value right before the initializing the Resource object pointed to it.

    But with the “atomics” feature of C++11 you may have a doubly checked locking mechanism.

    class Foo
    {
      Resource * resource;
      std::atomic<bool> isResourceNull;
    public:
      Foo() : resource(nullptr), isResourceNull(true) { }
    
      Resource & GetResource()
      {
        if(isResourceNull.load())
        {
          scoped_lock lock(mutex); 
          if(isResourceNull.load())
          {
            resource = new Resoruce();
            isResourceNull.store(false);
          }
        }
        return *resource;
      }
    }
    

    EDIT: Without atomics

    #include <winnt.h>
    
    class Foo
    {
      volatile Resource * resource;
    
      Foo() : resource(nullptr) { }
    public:
      Resource & GetResource()
      {
        if(resource == nullptr)
        {
          scoped_lock lock(mutex); 
          if(resource == nullptr)
          {
            Resource * dummy = new Resource();
            MemoryBarrier(); // To keep the code order
            resource = dummy;  // pointer assignment
          }
        }
        return  *const_cast<Resource*>(resource);
      }
    }
    

    MemoryBarrier() ensures that dummy will be first created then assigned to resource.
    According to this link pointer assignments will be atomic in x86 and x64 systems. And volatile ensures that the value of resource will not be cached.

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

Sidebar

Related Questions

That's the question. Give only one reason you think why have OODB failed or
That is, I'd like to have a tuple of values. The use case on
that one has been nagging me ever since I dabbled in web developpement; is
That's a really awkward title :) I need to write a report that generates
That's kinda vague so here's the meaty stuff: I have seen authentication systems that
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That shows: page execution time duration query string and form parameters SQL activity For
That's about DirectX 9. In typical case of rendering with vertex shader, there's one
That's basically it. I have a demo up here . I have been going

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.