4 ms·
> Fortunately, there’s an age-old solution to this problem - ring buffers. A ring buffer allows efficient synchronization between producers and consumers with n
by highfrequency 6y ago
> Fortunately, there’s an age-old solution to this problem - ring buffers. A ring buffer allows efficient synchronization between producers and consumers with no locking at all.
I don't think this is correct--ring buffers still require a mutex to prevent the producer from updating the write pointer while the reader checks the write pointer.
https://stackoverflow.com/questions/871234/circular-lock-free-buffer https://stackoverflow.com/questions/871234/circular-lock-fre...
https://stackoverflow.com/questions/52522512/do-i-need-a-mutex-when-using-a-circular-buffer-and-the-producer-consumer-desig https://stackoverflow.com/questions/52522512/do-i-need-a-mut...
- WJW 6y agoAs long as you have a reasonably modern CPU, you can use one of the various Compare-And-Swap (CAS) instructions to implement properly lock-free FIFO queues. There are a few variations but a circular array is one of the possibilities. We can probably discuss for quite some time if it's entirely lock-free if the lock is implemented in hardware but I think we can agree it's not a mutex :) The people over at enqueuezero have a nice article about lock free queues at https://enqueuezero.com/lock-free-queues.html https://enqueuezero.com/lock-free-queues.html.