
Writing Lock-Free Code: A Corrected Queue - DanielRibeiro
http://www.drdobbs.com/article/print?articleId=210604448&siteSectionName=parallel
======
sbahra
Please note that the code in this article is broken. It lacks proper use of
atomics, memory barriers, let alone compiler barriers. You can find a similar
and correct queue at <http://concurrencykit.org> (ck_fifo.h). The liblfds
queue is very heavy-weight for a simple SPSC workload and is also lacking in
correctness.

If you're looking for a learning resource, I suggest looking at
<http://www.rdrop.com/users/paulmck/perfbook/> and <http://1024cores.net>

~~~
liblfds_admin
sbahra is correct; at the very least, in release 6, the order of pointer and
counter reading is incorrect. This was fixed a long time ago, but release 7
hasn't yet come out; I've added an entry to the known issues page in the wiki.
The counter needs to be read first and not doing so permits ABA. The queue is
MPMC and as such is much heavier than the memory-barrier-only SPSC circular
buffer.

~~~
liblfds_admin
I should quantify this. The pointer/counter bug was fixed a long, long time
ago, but at the time slipped through the net with regard to being published on
the known issues page. That was a very unfortunate oversight. Sbahra emailed a
few weeks ago to inform me of the issue (and the others he raises) and I
checked the existing code base, to see the pointer/counter issue already fixed
and the other issues unclear (specifics were not given). I replied, but no
reply - but sbahra is a busy guy and also very competent, so I consider it
likely his comments have merit; I will look further into them once I've
finished my immediate release 7 work. With regard to adding the
pointer/counter issue to the known issues page, this I did today - I thought
about it earlier, but release 7 was is so close to coming out and the bug has
been present for so long, it seemed a pretty moot point (actually of course
that's completely untrue; people using six need to know, so they can know to
update their code).

------
rdtsc
I have used this library <http://www.liblfds.org/> for lock-free data
structures successfully. Only used the queue but it also has a linked-list
(logical deletion only), stack and a ring buffer.

There is another library others are using but can't remember the name
currently.

~~~
sparky
Concurrency Kit? <http://concurrencykit.org/>

~~~
rdtsc
Yap that's the one! Thank you.

------
tantaman
Java's ConcurrentLinkedQueue is lock free and supports any number of readers
and writers. For those that are interested:
[http://fuseyism.com/classpath/doc/java/util/concurrent/Concu...](http://fuseyism.com/classpath/doc/java/util/concurrent/ConcurrentLinkedQueue-
source.html)

And the original research: <http://www.cs.rochester.edu/u/michael/PODC96.html>

------
chj
Sometimes the producer has to insert something in the middle of the queue, is
it possible to make this queue lock free?

~~~
NeutronBoy
You could maintain separate queues for each priority, and always process them
from the top down.

~~~
chj
This sounds great for priority queues. But how about an event queue, where
every event has a time tag?

------
nkron
The follow up article in case anyone wanted to read it:
[http://www.drdobbs.com/parallel/writing-a-generalized-
concur...](http://www.drdobbs.com/parallel/writing-a-generalized-concurrent-
queue/211601363)

~~~
abcd_f
As a single page -
[http://www.drdobbs.com/article/print?articleId=211601363&...](http://www.drdobbs.com/article/print?articleId=211601363&siteSectionName=parallel)

