[Lock-free code is] hard even for experts. Itâs easy to write lock-free code that appears to work, but itâs very difficult to write lock-free code that is correct and performs well. Even good magazines and refereed journals have published a substantial amount of lock-free code that was actually broken in subtle ways and needed correction.
Thanks.Quote from rosy2:
i would also be interested to know about a lock free allocator.
i havent used c++ in a while but we used hoard; i dont think it was lock free though
Quote from rufus_4000:
I wrote a little library that uses the "cmpxchg" (for single processor) and the "lock cmpxchg" (for multi processor) assembler instructions, for both inter thread queuing and thread safe memory pooling (note I didn't say allocation). I wrote the whole thing in C, using a strange "template-like" grammar (using #define ... don't ask), and it scales quite well.
Look at the following paper on RCU (read-compare-update). It is sufficient for any single-producer, multiple consumer situations. The producer, however, may need to "spin-lock" (yield).
http://pdos.csail.mit.edu/6.828/2008/readings/rcu.pdf
Now, if it is just the simple one-producer, one consumer queue, you can play tricks like CPU-affinity, and just use volatile integers (Intel does a good job of putting up memory barriers, in the single processor, one producer / one-consumer case). And I have done exactly that, the performance there is about 25-30% gain from "lock cmpxchg", which does a strict "serialization" of operation on the variable.
you then have to call malloc (or gcnew) as a result