, . , .
vector, , , . " ", , , .
vector::operator[] , , . , vector::operator[] , , . - , concurrency ( , - LEA).
fetch_add , , , . , , , . , (.. "" ). , push.
pop, ( push) fetch_add (, , ) operator[].
, if(start==end) , operator[] . , - . :
- ( )
start, end ( ), , . - The next thread may increase
startafter this check, but before it is displayed fetch_addinside the call operator[], which will lead to indexing outside the bounds and invoke undefined behavior.
The non-intuitive thing to note here is that although you are doing the βright thingβ with atomic operations, the program will still behave differently, since not only individual operations must be atomic.
Damon source
share