mirror of
https://github.com/greg7mdp/parallel-hashmap.git
synced 2026-08-30 09:00:38 +08:00
Take care of the case where another thread inserts the same key between unlock()/lock()
This commit is contained in:
Vendored
+12
-3
@@ -3805,9 +3805,18 @@ protected:
|
||||
mutexlock = std::move(typename Lockable::ReadWriteLock(inner));
|
||||
size_t offset = set._find_key(key, hashval);
|
||||
if (offset == (size_t)-1) {
|
||||
mutexlock.switch_to_unique();
|
||||
offset = set.prepare_insert(hashval);
|
||||
return std::make_tuple(&inner, offset, true);
|
||||
if (mutexlock.switch_to_unique()) {
|
||||
// we did an unlock/lock, and another thread could have inserted the same key, so we need to
|
||||
// do a find() again.
|
||||
offset = set._find_key(key, hashval);
|
||||
if (offset == (size_t)-1) {
|
||||
offset = set.prepare_insert(hashval);
|
||||
return std::make_tuple(&inner, offset, true);
|
||||
}
|
||||
} else {
|
||||
offset = set.prepare_insert(hashval);
|
||||
return std::make_tuple(&inner, offset, true);
|
||||
}
|
||||
}
|
||||
return std::make_tuple(&inner, offset, false);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user