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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:29:34+00:00 2026-05-13T14:29:34+00:00

I’m in the process of writing a template library for data-caching in C++ where

  • 0

I’m in the process of writing a template library for data-caching in C++ where concurrent read can be done and concurrent write too, but not for the same key. The pattern can be explained with the following environment:

  1. A mutex for the cache write.
  2. A mutex for each key in the cache.

This way if a thread requests a key from the cache and is not present can start a locked calculation for that unique key. In the meantime other threads can retrieve or calculate data for other keys but a thread that tries to access the first key get locked-wait.

The main constraints are:

  1. Never calculate the value for a key at the same time.
  2. Calculating the value for 2 different keys can be done concurrently.
  3. Data-retrieval must not lock other threads from retrieve data from other keys.

My other constraints but already resolved are:

  1. fixed (known at compile time) maximum cache size with MRU-based ( most recently used ) thrashing.
  2. retrieval by reference ( implicate mutexed shared counting )

I’m not sure using 1 mutex for each key is the right way to implement this but i didn’t find any other substantially different way.

Do you know of other patterns to implements this or do you find this a suitable solution? I don’t like the idea of having about 100 mutexs. ( the cache size is around 100 keys )

  • 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-13T14:29:34+00:00Added an answer on May 13, 2026 at 2:29 pm

    You want to lock and you want to wait. Thus there shall be “conditions” somewhere (as pthread_cond_t on Unix-like systems).

    I suggest the following:

    • There is a global mutex which is used only to add or remove keys in the map.
    • The map maps keys to values, where values are wrappers. Each wrapper contains a condition and potentially a value. The condition is signaled when the value is set.

    When a thread wishes to obtain a value from the cache, it first acquires the global mutex. It then looks in the map:

    1. If there is a wrapper for that key, and that wrapper contains a value, then the thread has its value and may release the global mutex.
    2. If there is a wrapper for that key but no value yet, then this means that some other thread is currently busy computing the value. The thread then blocks on the condition, to be awaken by the other thread when it has finished.
    3. If there is no wrapper, then the thread registers a new wrapper in the map, and then proceeds to computing the value. When the value is computed, it sets the value and signals the condition.

    In pseudo code this looks like this:

    mutex_t global_mutex
    hashmap_t map
    
    lock(global_mutex)
    w = map.get(key)
    if (w == NULL) {
        w = new Wrapper
        map.put(key, w)
        unlock(global_mutex)
        v = compute_value()
        lock(global_mutex)
        w.set(v)
        signal(w.cond)
        unlock(global_mutex)
        return v
    } else {
        v = w.get()
        while (v == NULL) {
            unlock-and-wait(global_mutex, w.cond)
            v = w.get()
        }
        unlock(global_mutex)
        return v
    }
    

    In pthreads terms, lock is pthread_mutex_lock(), unlock is pthread_mutex_unlock(), unlock-and-wait is pthread_cond_wait() and signal is pthread_cond_signal(). unlock-and-wait atomically releases the mutex and marks the thread as waiting on the condition; when the thread is awaken, the mutex is automatically reacquired.

    This means that each wrapper will have to contain a condition. This embodies your various requirements:

    • No threads holds a mutex for a long period of time (either blocking or computing a value).
    • When a value is to be computed, only one thread does it, the other threads which wish to access the value just wait for it to be available.

    Note that when a thread wishes to get a value and finds out that some other thread is already busy computing it, the threads ends up locking the global mutex twice: once in the beginning, and once when the value is available. A more complex solution, with one mutex per wrapper, may avoid the second locking, but unless contention is very high, I doubt that it is worth the effort.

    About having many mutexes: mutexes are cheap. A mutex is basically an int, it costs nothing more than the four-or-so bytes of RAM used to store it. Beware of Windows terminology: in Win32, what I call here a mutex is deemed an “interlocked region”; what Win32 creates when CreateMutex() is called is something quite different, which is accessible from several distinct processes, and is much more expensive since it involves roundtrips to the kernel. Note that in Java, every single object instance contains a mutex, and Java developers do not seem to be overly grumpy on that subject.

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

Sidebar

Related Questions

I want to construct a data frame in an Rcpp function, but when I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string

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.