cmap: Fix data race reported by ThreadSanitizer #22
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There are some data races on the
cmap
example, and this pull request tries to fix it.Most of the race can be simply fixed by applying atomic operation. For example, the synchronization on the counter or status variables. However, there are still fixes that may have to be considered carefully. As the behavior may be different from the original implementation.
One of them is the race of random
seed
. In my opinion, I think it should make more sense to maintain a thread-independent seed for each thread. Thus the seed is changed to a thread local storage variable.Another hard problem is the race between the removed part of the writer thread and the other reader threads. It is dangerous because the writer could free the memory that is still used by readers, which causes use-after-free bugs. This is not a simple question. If you want an elegant and even efficient solution, it may be ideal to introduce some high-level data structure such as the hazard pointer. But considering that this project should just provide an easy-to-use example, I choose a simple and crude method: maintain a linked list of memory that should be released, and not release it until the end of the program. Although this is obviously not a smart way, it is at least the simplest way to ensure correctness.