Is there a reason why you can't access the concurrent_hash_map using the square bracket operators?
I've been doing this in order to ease the code readability (on keys that should be in the map):
template <class Tkey, class Tval>
Tval concHashMapGet(concurrent_hash_map < Tkey, Tval >& chm , Tkey key)
{
concurrent_hash_map< Tkey, Tval >::const_accessor a;
if (chm.find(a, key))
return a->second;
else
throw;
}; //Will .release() when out of scope
And I was wondering whether I'd missed something about the proper usage, because it seems you need to get an accessor, then run find, then get the value, then release the accessor. All of which in the normal map or the ConcurrentDictionary in c# is done using just the square bracket operator. (Well I guess there's no synchronization in the STL map, but I'm after the square brackets.)
Also if you spot any issues with this function, let me know. As far as I can tell, the compiler should inline it?
Your logic looks right, and there's no shorter way to do it as far as I know. The reason concurrent_hash_map does not have operator[] is that if it returned a reference as in std::map::opreator[], we would have to choose a default locking (accessor or const_accessor), and whichever we chose, it could be the wrong choice in some use cases. So we forced the caller to make the choice.
If you do not need to erase items concurrently, consider using tbb::concurrent_unordered_map instead. It's a more recent design that has a lock-free interface, and does have operator[].
With respect to the code example, the "throw;" needs to throw something unless the code is always called from inside a try-block handler.