Looks correct to me. It will not suffer from ABA, because you create a new node on each push, and then give it to GC. While a goroutine holds a reference to A in pushed.next, GC will not collect A, and so A cannot appear again in stack.next.
On Fri, Dec 5, 2014 at 2:35 AM, Adam <[email protected]> wrote: > please critique my mpmc stack implementation. I am unsure about a few > things. First, have I used the unsafe package correctly? Second, will this > implementation suffer the ABA problem? > > > type Stack struct { > next unsafe.Pointer > } > > type Element struct { > next unsafe.Pointer > value int > } > > func (stack *Stack) Push(value int) (b bool) { > pushed := Element{value: value} > up_pushed := unsafe.Pointer(&pushed) > for { > pushed.next = atomic.LoadPointer(&stack.next) > if atomic.CompareAndSwapPointer(&stack.next, pushed.next, up_pushed) { > b = true > return > } > } > return > } > > func (stack *Stack) Pop() (b bool, value int) { > for { > up_popped := atomic.LoadPointer(&stack.next) > if up_popped == nil { > return > } > popped := *(*Element)(unsafe.Pointer(up_popped)) > if atomic.CompareAndSwapPointer(&stack.next, up_popped, popped.next) { > b = true > value = popped.value > popped.next = nil > return > } > > } > return > } > > -- > > --- > You received this message because you are subscribed to the Google Groups > "Scalable Synchronization Algorithms" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/lock-free/b94e7686-57e3-4cc8-9697-9e51f5085750%40googlegroups.com. > For more options, visit https://groups.google.com/d/optout. -- Dmitry Vyukov All about lockfree/waitfree algorithms, multicore, scalability, parallel computing and related topics: http://www.1024cores.net -- --- You received this message because you are subscribed to the Google Groups "Scalable Synchronization Algorithms" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/lock-free/CAEeQi3tEoU5nVVTaDb6ZX-P6CxaMtkKHpgHqz18eQjVhkz8eGQ%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
