mirror of
https://github.com/greg7mdp/parallel-hashmap.git
synced 2026-08-30 17:10:40 +08:00
Make a single if_contains function to simplify the interface
This commit is contained in:
@@ -287,7 +287,7 @@ Parallel Hashmap containers follow the thread safety rules of the Standard C++ l
|
||||
|
||||
- It is safe to read and write to one instance of a type even if another thread is reading or writing to a different instance of the same type. For example, given hash tables A and B of the same type, it is safe if A is being written in thread 1 and B is being read in thread 2.
|
||||
|
||||
- The *parallel* tables can be made internally thread-safe for concurrent read and write access, by providing a synchronization type (for example [std::mutex](https://en.cppreference.com/w/cpp/thread/mutex)) as the last template argument. Because locking is performed at the *submap* level, a high level of concurrency can still be achieved. Read access can be done safely using `if_contains()`, which either copies the value or passes it by reference to a callback while holding the *submap* lock. However, please be aware that returned iterators are not protected by the mutex, so they cannot be used reliably on a hash map which can be changed by another thread.
|
||||
- The *parallel* tables can be made internally thread-safe for concurrent read and write access, by providing a synchronization type (for example [std::mutex](https://en.cppreference.com/w/cpp/thread/mutex)) as the last template argument. Because locking is performed at the *submap* level, a high level of concurrency can still be achieved. Read access can be done safely using `if_contains()`, which passes a reference value to the callback while holding the *submap* lock. However, please be aware that returned iterators are not protected by the mutex, so they cannot be used reliably on a hash map which can be changed by another thread.
|
||||
|
||||
- Examples on how to use various mutex types, including boost::mutex, boost::shared_mutex and absl::Mutex can be found in `examples/bench.cc`
|
||||
|
||||
|
||||
Vendored
+2
-14
@@ -3393,23 +3393,11 @@ public:
|
||||
return Policy::value(&*it);
|
||||
}
|
||||
|
||||
template <class K = key_type, class V = mapped_type>
|
||||
typename std::enable_if<std::is_assignable<mapped_type&, V>::value
|
||||
, bool>::type if_contains(const key_arg<K>& key, V& v) const {
|
||||
typename Lockable::SharedLock m;
|
||||
auto it = const_cast<parallel_hash_map *>(this)->find(key, this->hash(key), m);
|
||||
if (it == this->end())
|
||||
return false;
|
||||
v = Policy::value(&*it);
|
||||
return true;
|
||||
}
|
||||
|
||||
template <class K = key_type, class F>
|
||||
typename std::enable_if<!std::is_assignable<mapped_type&, F>::value
|
||||
bool if_contains(const key_arg<K>& key, F&& f) const {
|
||||
#if __cplusplus >= 201703L
|
||||
&& std::is_invocable<F, mapped_type&>::value
|
||||
static_assert(std::is_invocable<F, mapped_type&>::value);
|
||||
#endif
|
||||
, bool>::type if_contains(const key_arg<K>& key, F&& f) const {
|
||||
typename Lockable::SharedLock m;
|
||||
auto it = const_cast<parallel_hash_map*>(this)->find(key, this->hash(key), m);
|
||||
if (it == this->end())
|
||||
|
||||
@@ -14,19 +14,12 @@ TEST(THIS_TEST_NAME, ThreadSafeContains) {
|
||||
// Test that the nodes have the proper API.
|
||||
ThisMap<int, int> m = { {1, 7}, {2, 9} };
|
||||
auto val = 0;
|
||||
EXPECT_TRUE(m.if_contains(1, val));
|
||||
EXPECT_EQ(val, 7);
|
||||
|
||||
EXPECT_FALSE(m.if_contains(3, val));
|
||||
|
||||
#if __cplusplus > 199711L
|
||||
auto func = [&val](int& v) { val = v; };
|
||||
EXPECT_TRUE(m.if_contains(2, func));
|
||||
EXPECT_EQ(val, 9);
|
||||
|
||||
EXPECT_FALSE(m.if_contains(3, func));
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Reference in New Issue
Block a user