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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:14:12+00:00 2026-05-29T20:14:12+00:00

I am interfacing with a back-end system, where I must never ever have more

  • 0

I am interfacing with a back-end system, where I must never ever have more than one open connection to a given object (identified by it’s numeric ID), but different consumers may be opening and closing them independently of one another.

Roughly, I have a factory class fragment like this:

private Dictionary<ulong, IFoo> _openItems = new Dictionary<ulong, IFoo>();
private object _locker = new object();

public IFoo Open(ulong id)
{
    lock (_locker)
    {
        if (!_openItems.ContainsKey(id))
        {
            _openItems[id] = _nativeResource.Open(id);
        }

        _openItems[id].RefCount++;

        return _openItems[id];
    }
}

public void Close(ulong id)
{
    lock (_locker)
    {
        if (_openItems.ContainsKey(id))
        {
            _openItems[id].RefCount--;
            if (_openItems[id].RefCount == 0)
            {
                _nativeResource.Close(id);
                _openItems.Remove(id);
            }
        }
    }
}

Now, here is the problem. In my case, _nativeResource.Open is very slow. The locking in here is rather naive and can be very slow when there are a lot of different concurrent .Open calls, even though they are (most likely) referring to different ids and don’t overlap, especially if they are not in the _openItems cache.

How do I structure the locking so that I am only preventing concurrent access to a specific ID and not to all callers?

  • 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-29T20:14:13+00:00Added an answer on May 29, 2026 at 8:14 pm

    What you may want to look into is a striped locking strategy. The idea is that you share N locks for M items (possible ID’s in your case), and choose a lock such that for any ID the lock chosen is always the same one. The classic way of choosing locks for this technique is modulo division- simply divide M by N, take the remainder, and use the lock with that index:

    // Assuming the allLocks class member is defined as follows:
    private static AutoResetEvent[] allLocks = new AutoResetEvent[10];
    
    
    // And initialized thus (in a static constructor):
    for (int i = 0; i < 10; i++) {
        allLocks[i] = new AutoResetEvent(true);
    }
    
    
    // Your method becomes
    var lockIndex = id % allLocks.Length;
    var lockToUse = allLocks[lockIndex];
    
    // Wait for the lock to become free
    lockToUse.WaitOne();
    try {
        // At this point we have taken the lock
    
        // Do the work
    } finally {
        lockToUse.Set();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Our application is interfacing with a lot of web services these days. We have
I have implemented a simple appmod that handle WebSockets and echo back the messages.
Hypothetically, you have two products, one written in Java, the other in C#. You
Me and my team are developing an application, which involves a back-end written in
I have a one page web app so there is no page refreshing. Sometimes
I have a somewhat complex iOS view hierarchy. One piece of text is an
Im perplexed by this one. jQuery.height() is coming back with different values in Firefox
Basically my app is interacting with a web service that sends back a weird
I'm interfacing with a payment gateway and not having any luck with Net::SSLeay and
I am interfacing with a server that requires that data sent to it is

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.