thread-safe-lru icon indicating copy to clipboard operation
thread-safe-lru copied to clipboard

Provide APIs that accept user passed accessor as a param?

Open jy5275 opened this issue 5 years ago • 0 comments

I use your excellent implementation of scalable-cache as a counter. The instantiated value type is int. The counter needs to be increase by 1 like this:

template<typename T>
void AddCount(int idx, T key) {
  ScalableCache::ConstAccessor ac;
  if (scala_counter.find(ac, key)) {
    int cnt = *ac;
    scala_counter.insert(key, cnt+1);
  } else {
    scala_counter.insert(key, 1);
  }
}

This causes problem. In find method, accessor is passed by user as a param. While insert method creates its own accessor. Deadlock occurs when insert tries to aquire a lock on key entry but find is still holding it. There seems to be no graceful solution with current APIs. And the control of accessor may confuse programmers. I think we could add the following APIs:

  bool find(HashMapConstAccessor& ac, const TKey& key);
  bool find(HashMapAccessor& ac, const TKey& key);

  bool insert(const TKey& key, const TValue& value);
  bool insert(HashMapAccessor &ac, const TKey& key, const TValue& value);

With these APIs, programmer could explicitly synchronizes the threads that access the same cache object. What do you think?

jy5275 avatar Sep 03 '20 02:09 jy5275