The order of operations in
`````````````````
list_head.compare_exchange_weak( oldHead, oldHead->next ) // part of pop
`````````````````
This is a function call, before the actual call (which will be atomic) all arguments have to be evaluated,
thus
1. evaluate `oldHead->next ` (will be placed in some temporary memory)
2. call `compare_exchange_weak `
Thread may be interrupted between 1) and 2)
Example of ABA:
---------------
Original list:
************************************************
* 20 30 40 50 *
* +-----+ +-----+ +-----+ +-----+ *
* | +->| +->| +->| +-> *
* +-----+ +-----+ +-----+ +-----+ *
* ^ *
* head | *
* +-----+ | *
* | 20 +--+ *
* +-----+ *
************************************************
T1 start a pop:
````````````
location = head
old state = 20
desired = 30 // next of the current head -- see note above
````````````
T2 kicks in:
T2 pop
**************************************************
* 30 40 50 *
* +-----+ +-----+ +-----+ *
* | +->| +->| +-> *
* +-----+ +-----+ +-----+ *
* ^ *
* head | *
* +-----+ | *
* | 30 +---+ *
* +-----+ *
**************************************************
T2 push
**************************************************
* 60 30 40 50 *
* +-----+ +-----+ +-----+ +-----+ *
* | +->| +->| +->| +-> *
* +-----+ +-----+ +-----+ +-----+ *
* ^ *
* head | *
* +-----+ | *
* | 60 +---+ *
* +-----+ *
**************************************************
T2 push (this push reuses address 20)
***********************************************************
* 20 60 30 40 50 *
* +-----+ +-----+ +-----+ +-----+ +-----+ *
* | +->| +->| +->| +->| +-> *
* +-----+ +-----+ +-----+ +-----+ +-----+ *
* ^ *
*head | *
*+-----+ | *
*| 20 +-----+ *
*+-----+ *
***********************************************************
T1 continues:
``````````
location == old state // true - success
head = desired = 30 !!!!!
``````````
*************************************************************
* 60 30 40 50 *
* +-----+ +-----+ +-----+ +-----+ *
* | +->| +->| +->| +-> *
* +-----+ +-----+ +-----+ +-----+ *
* ^ *
* head | *
* +-----+ | *
* | 30 |------------+ *
* +-----+ *
*************************************************************
**Two-headed** linked list!
Another example of ABA:
-----------------------
Original list:
*****************************************************
* 20 30 40 50 *
* +-----+ +-----+ +-----+ +-----+ *
* | +->| +->| +->| +-> *
* +-----+ +-----+ +-----+ +-----+ *
* ^ *
* head | *
* +-----+ | *
* | 20 +---+ *
* +-----+ *
*****************************************************
T1 start a pop:
location = head
old state = 20
desired = 30 (next of the current head)
T2 kicks in:
T2 pop
******************************************************
* 30 40 50 *
* +-----+ +-----+ +-----+ *
* | +->| +->| +-> *
* +-----+ +-----+ +-----+ *
* ^ *
* head | *
* +-----+ | *
* | 30 +---+ *
* +-----+ *
******************************************************
T2 pop
******************************************************
* 40 50 *
* +-----+ +-----+ *
* | +->| +-> *
* +-----+ +-----+ *
* ^ *
* head | *
* +-----+ | *
* | 40 +---+ *
* +-----+ *
******************************************************
T2 push (this push reuses address 20)
******************************************************
* 20 40 50 *
* +-----+ +-----+ +-----+ *
* | +->| +->| +-> *
* +-----+ +-----+ +-----+ *
* ^ *
* head | *
* +-----+ | *
* | 20 +---+ *
* +-----+ *
******************************************************
T1 continues:
`````````````````
location == old state // true - success
head = desired = 30 !!!!!
`````````````````
******************************************************
* 60 40 50 *
* +-----+ +-----+ +-----+ *
* | +->| +->| +-> *
* +-----+ +-----+ +-----+ *
* *
*head *
*+-----+ *
*| 30 |--------------> points to deleted memory *
*+-----+ *
******************************************************