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

Thread safety for changing values of keys already in ordered_map #46

Open
ericonr opened this issue Feb 1, 2024 · 3 comments
Open

Thread safety for changing values of keys already in ordered_map #46

ericonr opened this issue Feb 1, 2024 · 3 comments

Comments

@ericonr
Copy link

ericonr commented Feb 1, 2024

Hi!

I'm using this project's ordered_map. The values under the keys can change, but after the map is first populated, the keys always remain the same. In that situation, in a multithreaded program, is it safe to overwrite the value under one key from thread 1 while accessing another key from thread 2?

I took a look at https://tessil.github.io/ordered-map/classtsl_1_1ordered__map.html#details

insert, emplace, emplace_hint, operator[]: when a std::vector is used as ValueTypeContainer and if size() < capacity(), only end(). Otherwise all the iterators are invalidated if an insert occurs.

From that quote, is it correct that simply changing the value under one key isn't an "insertion" (as defined in something like cplusplus docs for std::unordered_map: adding a new element to the map) and therefore even an ordered_map using std::deque isn't invalidating its iterators (and therefore, can access values under other keys from another thread safely)?

If my understanding is correct and you think it makes sense, I could try my hand at updating the docs to make them clearer on this point.

Cheers!

@ericonr
Copy link
Author

ericonr commented Mar 28, 2024

@Tessil ping :)

@Tessil
Copy link
Owner

Tessil commented Mar 31, 2024

Hi @ericonr

Sorry for the delay. Yes, your understanding is right.

Using insert or emplace on an existing key will not modify the map and as long as both threads are not trying to read and update the value of the same key, everything should be thread safe.

The documentation could effectively be updated to make the distinction between an actual insert and an insert call with an existing key (which will just return an iterator to the existing element similarly to find).

@ericonr
Copy link
Author

ericonr commented Apr 3, 2024

Sorry for the delay. Yes, your understanding is right.

No worries, I know how easily things can slip up :) And that's great to know!

The documentation could effectively be updated to make the distinction between an actual insert and an insert call with an existing key (which will just return an iterator to the existing element similarly to find).

I will try and find the time to make a PR, then :)

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