Re: Writing a lock-free, threadsafe stack (or queue)

2019-03-01 Thread Hongwei Xi
I took a quick look. The paper gives an algorithm for implementing a queue, and your code implements a stack. The stack implementation contains a few race conditions. For example, say that thread A pops and thread B pops as well; after thread A frees a node, thread B could try to free it again,

Re: Writing a lock-free, threadsafe stack (or queue)

2019-03-01 Thread Richard
At first glance, making the functions polymorphic (as opposed to templates) could be a potential reason for segmentation faults (see https://github.com/githwxi/ATS-Postiats/issues/216). Though mixing dependent types and templates together has also been known to cause issues (see

Re: Stack-allocated arrays in ATS2 (or ATS3?)

2019-03-01 Thread gmhwxi
One very serious danger with using alloca is that alloca interferes with tail-call optimization. After debugging code using alloca a couple times, I would be really hard pressed to every use alloca again. On Saturday, February 23, 2019 at 6:24:24 AM UTC-5, Artyom Shalkhakov wrote: > > Hi

Re: Writing a lock-free, threadsafe stack (or queue)

2019-03-01 Thread Vanessa McHale
I don't think it's due to polymorphic functions, because the problem goes away when I switch to using only one thread. Cheers, Vanessa On 3/1/19 8:43 PM, Richard wrote: > At first glance, making the functions polymorphic (as opposed to templates) > could be a potential reason for segmentation

Re: Writing a lock-free, threadsafe stack (or queue)

2019-03-01 Thread gmhwxi
If we ignore malloc/free, then lock-free stack implementation should look more or less like the following code: fun pop(theStack) = let var xs0 = theStack in case+ xs0 of | nil() => None_vt() | cons(x0, xs1) => if atomic_compare_exchange(theStack, xs0, xs1) then Some_vt(x0)