1
votes

I'd like to implement a lockless single-producer, single-consumer circular queue between two pthreads; in C, on ARM Linux.

The queue will hold bytes, the producer will memcpy() things in, and the consumer will write() them out to file.

Is it naive to think I can store head and tail offsets in ints and everything will just work? I am wondering about such things as compiler optimisations meaning my head/tail writes sit in registers and are not visible to the other thread, or needing a memory barrier somewhere.

1

1 Answers

4
votes

The memory consistency model of pthreads doesn't offer you any assistance for building lockless algorithms. You're on your own - you will have to use whatever atomic instructions and memory barriers are provided and required by your architecture. You'll also have to consult your compiler documentation to determine how to request a compiler barrier.

You are almost certainly better off using a normal queue implementation protected by a mutex and condition variable - if the queue simply stores pointers to the buffers that are being written out to file (rather than the data itself), then lock contention shouldn't be a problem, as the lock will only have to be held while a pointer is added or removed from the queue.