简体   繁体   中英

Concurrent cache

I'm looking for a concurrent cache structure. I'm using the PPL from Microsoft so I have the concurrent_unordered_map class, but it doesn't quite seem to be what I need. I've got a hash value and I need to associate it with a pointer type, or return that pointer if it was already in the cache. I'm not using LRU or MRU cache strategy and values wil never be removed, so it's more like a concurrent memoize.

Would it be simpler to just lock an existing std::unordered_map ?

I don't know about the Microsoft PPL. I just looked at the Intel Thread Building Blocks header files for Intel's concurrent_unordered_map and its insert function returns false as the second part of the returned pair when the key is already in the map.

This seems to be exactly what you need. Do the insert and if it returns true then it was a new insert. If it returns false then it was already in the map.

Edit: There seems to be some confusion here. I didn't mean that you should always run the insert. I meant that you should look for the value and if it is missing then attempt the insert. Two or more threads may occasionally race on the insert and so work will be duplicated, but this should be a rare event.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM