"non-blocking thread-safe queue in c++?" Code Answer


assuming your cpu has a double-pointer-wide compare-and-swap (compxchg8b on 486 or higher, compxchg16b on most amd64 machines [not present on some early models by intel])... there is an algorithm here.

update: it's not hard to translate this to c++ if you aren't afraid of doing a bit of work. :p

this algorithm assumes a "pointer with tag" structure which looks like this:

// be aware that copying this structure has to be done atomically...

template <class t>
struct pointer
   t *ptr;
   uintptr_t tag;

then you want to wrap the instructions lock cmpxchg{8|16}b with some inline asm...

maybe then you can write the queue node like this:

template <class t>
struct queue_node
    t value;
    pointer<queue_node<t> > next;

the rest is more or less a transcription of the algorithm i linked to...

By NatFar on February 28 2022

Answers related to “non-blocking thread-safe queue in c++?”

Only authorized users can answer the Search term. Please sign in first, or register a free account.