Fix race condition in P1.memo()#106
Merged
Merged
Conversation
The race was when two thread encountered the uninitialized state, `a == null`. One thread gets suspended before entering the `synchronized` block, while the other runs though it. The latter thread sets `v` to something not null. Then the first thread resumes, enters the `synchronized` block and checks if `(v == null || v.get() == null)` which is now false, and thus returns the still uninitialized `a`. Fixes functionaljava#105
Contributor
|
Thanks for your fast response @knutwalker. I read through your reasoning with the code and believe you are correct. |
mperry
added a commit
that referenced
this pull request
Feb 11, 2015
Fix race condition in P1.memo()
|
The code looks to me like the caching doesn't work for a _1() will that returns null, i.e. repeated calls to memo() will repeatedly evaluate _1(), contributing to the quadratic execution time that memos were created to avoid. Maybe the condition should only be based on v == null -- something like this? |
Contributor
|
I created #108 to deal with this. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The race was when two thread encountered the uninitialized state,
a == null.One thread gets suspended before entering the
synchronizedblock, while the other runs though it. The latter thread setsvto something not null. Then the first thread resumes, enters thesynchronizedblock and checks if(v == null || v.get() == null)which is now false, and thus returns the still uninitializeda.Fixes #105