Listing 7.3 - add early exit to .pop()
#36
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.
It seems odd to perform work on a node that could potentially be a
nullptrfrom the moment the function is called.I hope I'm correct in saying that
.compare_exchange_weak(...)will returntrueifheadstill refers to our originalold_head(i.e. no other thread has had a chance to modify it), but if thatold_headwasnullptrto begin with, then wouldold_head->nextnot throw after a successful CAS check?ccia_code_samples/listings/listing_7.3.cpp
Lines 24 to 30 in 6134dee
The same thing applies to
void pop(T &val)a few pages prior - an early exit allows the code the run without throwing for bad access toold_head->next.Arguably, maybe changing
old_headtooriginal_headwould be more conducive to helping readers grasp the concept of.compare_exchange_weak(...).