Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add try_emplace_p extension method #206

Merged
merged 1 commit into from
Sep 12, 2023
Merged

Conversation

ecatmur
Copy link
Contributor

@ecatmur ecatmur commented Sep 11, 2023

Iterators are invalidated by concurrent insertion (rehash, resize, etc...) whereas for node-based containers, pointers are only invalidated by erase/clear.

e.g. this program causes a data race (read vs. reallocate) w/ tsan (g++ 11.4.0-1ubuntu1~22.04 -fsanitize=thread):

#include <parallel_hashmap/phmap.h>
#include <thread>
#include <mutex>
int main() {
    phmap::parallel_node_hash_map<int, int, std::hash<int>, std::equal_to<int>, std::allocator<std::pair<int const, int>>, 4, std::mutex> map;
    std::thread t1([&] { for (int i = 0; i != 10; ++i) ++map.try_emplace(i, i).first->second; });
    std::thread t2([&] { for (int i = 10; i != 20; ++i) ++map.try_emplace(i, i).first->second; });
    t1.join();
    t2.join();
}

Applying this PR and changing try_emplace to try_emplace_p fixes it.

Iterators are invalidated by concurrent insertion (rehash, resize, etc...) whereas for node-based containers, pointers are only invalidated by erase/clear.
Copy link
Owner

@greg7mdp greg7mdp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot, Ed, this is a good idea and I really appreciate the contribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants