views:

74

answers:

5

I was thinking, is it possible to have a lockless queue when more then one thread is reading or writing? I seen an implementation with a lockless queue that worked with one read and one write thread but never more then one for either. Is it possible? I don't think it is. Can/does anyone want to prove it?

A: 

You don't specifically need a lock, but an atomic way of deleting things from the queue. This is also possible without a lock and with an atomic test-and-set instruction.

Sjoerd
Is that a single CPU instruction?
acidzombie24
Assembly instruction CMPXCHG
Sjoerd
+1  A: 

With .NET 4.0, there is ConcurrentQueue(T) Class.
According to C# 4.0 in a nutshell, this is a lock free implementation. See also this blog entry.

weismat
+1  A: 

There are multiple algorithms available, I ended up implementing the An Optimistic Approach to Lock-Free FIFO Queues, which avoids the ABA problem via pointer-tagging (needs the CMPXCHG8B instruction on x86), and it runs fine in a production app (written in Delphi). (Another version, with Java code)

Nevertheless, to be really-really lockless, you would also need a lock-free memory allocator - see Scalable Lock-Free Dynamic Memory Allocation (implemented in Concurrent Building Block) or NBMalloc (but so far, I didn't get to use one of these).

You may also want to look at answers for optimistic lock-free FIFO queues impl?

Viktor Svub
I didnt understand CAS until i read the CMPXCHG instruction. The CMPXCHG instruction looks cool, so i take it if its a success it will set the Z flag? Now after minutes of confusion with CAS until reading CMPXCHG CAS looks like CAS(dst, oldValue, newVal); where dst is the address destination and the other two are values? returning true if dst was old value and has been now replace by newValue? (I know i said a lot but i think i got this right, it seems very right).
acidzombie24
A: 

There is a dynamic lock free queue in the OmniThreadLibrary by Primoz Gabrijelcic (the Delphi Geek): http://www.thedelphigeek.com/2010/02/omnithreadlibrary-105.html

Marjan Venema
+1  A: 

Java's implementation of a Lockless Queue allows both reads and writes. This work is done with a compare and set operation (which is a single cpu instruction).

The ConcurrentLinkedQueue uses a method in which threads help eacthoer read (or poll) objects from the queue. Since it is linked, the the head of the queue can accept writes while the tail of the queue can accept reads (assuming enough room). All of this can be done in parallel and is completely thread safe

John V.