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

about a concurrency operation issue #248

Closed
littledoglily opened this issue Sep 9, 2024 · 4 comments
Closed

about a concurrency operation issue #248

littledoglily opened this issue Sep 9, 2024 · 4 comments

Comments

@littledoglily
Copy link

"I have a background thread which consumes Kafka messages and then writes them into a parallel_flat_hash_map. Other threads execute find operations. I really want to know if the parallel_flat_hash_map must be protected by a mutex. In our system, we define the map like this: using LocalMap = phmap::parallel_flat_hash_map<uint64_t, std::array<float, N>>, which uses the default NullMutex, and we have not experienced any core dumps or segmentation faults.

However, in the README, I found the following statement: 'If a single hash table is being written to by one thread, then all reads and writes to that hash table on the same or other threads must be protected. For example, given a hash table A, if thread 1 is writing to A, then thread 2 must be prevented from reading from or writing to A.'

I have some confusion, and I am not entirely sure if I understand correctly.

@greg7mdp
Copy link
Owner

greg7mdp commented Sep 9, 2024

Yes, it sounds like you should definitely use std::mutex instead of phmap::NullMutex in your map declaration. I would think that you'd get seg faults though, surprising you don't.

@littledoglily
Copy link
Author

littledoglily commented Sep 9, 2024

Yes, it sounds like you should definitely use std::mutex instead of phmap::NullMutex in your map declaration. I would think that you'd get seg faults though, surprising you don't.

Thanks for your response.
Another question is: if I dump a phmap::parallel_flat_hash_map<uint64_t, std::array<float, N>>, can I load it with phmap::parallel_flat_hash_map<uint64_t, std::array<float, N>, hmap::priv::hash_default_hash<uint64_t>, phmap::priv::hash_default_eq<uint64_t>, phmap::priv::Allocator<phmap::priv::Pair<uint64_t, std::array<float, N>>>, 4, std::mutex std::mutex>? Or must the map used for loading and dumping be declared the same way?

@greg7mdp
Copy link
Owner

greg7mdp commented Sep 9, 2024

Yes, I think it should work fine, the mutex type does not have to match.
Also, as an alternative if needed, you can swap two parallel_flat_hash_map whose mutex type differ.

@littledoglily
Copy link
Author

Yes, I think it should work fine, the mutex type does not have to match. Also, as an alternative if needed, you can swap two parallel_flat_hash_map whose mutex type differ.

Thanks for your help.

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

No branches or pull requests

2 participants