C ++ Stream Cache Object Cache Design Options

Now I am writing a template library for caching data in C ++, where you can perform parallel reading and simultaneous writing, but not for the same key. The template can be explained using the following environment:

  • Mutex for writing cache.
  • Mutex for each key in the cache.

Thus, if a thread requests a key from the cache and is absent, it can start a blocked calculation for this unique key. Meanwhile, other threads can retrieve or compute data for other keys, but a thread that tries to access the first key gets a wait-lock.

The main limitations are:

  • Never calculate the value for a key at the same time.
  • The calculation of the value for two different keys can be performed simultaneously.
  • Data retrieval should not block other threads from retrieving data from other keys.

My other limitations, but already allowed:

  • fixed (known at compile time) maximum cache size using the last (last) used).
  • search by reference (estimated total mutex count)

I'm not sure that using 1 mutex for each key is the right way to implement this, but I have not found another significantly different way.

Do you know that other templates implement this, or do you find this a suitable solution? I don't like the idea of ​​having about 100 mutex. (cache size is about 100 keys)

+3
source share
3 answers

. , - "" ( pthread_cond_t Unix- ).

:

  • , .
  • , - . . , .

, . :

  • , , .
  • , , , - . , , .
  • , . , .

:

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
}

pthreads lock pthread_mutex_lock(), unlock is pthread_mutex_unlock(), unlock-and-wait is pthread_cond_wait() signal is pthread_cond_signal(). unlock-and-wait ; , .

, . :

  • ( ).
  • , , , , .

, , - , : , . , , , , .

: . int, , , . Windows: Win32 , , " "; , Win32 CreateMutex(), - , , . , Java , Java, , .

+2

. . , , . , , . , , .

+3

, , / . , , ( ), , - "" . :

acquire read lock
search for key
if found
    use the key
else
    release read lock
    acquire write lock
    add key
    release write lock
    // acquire the read lock again and use it (probably encapsulate in a method)
endif

, , . , , .

0

Source: https://habr.com/ru/post/1730046/


All Articles