3 ms·
Here's a good explanation from Linux Device Drivers, Third Edition. "When carefully implemented, a circular buffer requires no locking in the absence of multip
by learc83 8y ago
Here's a good explanation from Linux Device Drivers, Third Edition.
"When carefully implemented, a circular buffer requires no locking in the absence of multiple producers or consumers. The producer is the only thread that is allowed to modify the write index and the array location it points to. As long as the writer stores a new value into the buffer before updating the write index, the reader will always see a consistent view. The reader, in turn, is the only thread that can access the read index and the value it points to. With a bit of care to ensure that the two pointers do not overrun each other, the producer and the consumer can access the buffer concurrently with no race conditions."
- thebruce87m 8y agoThis would rely on an atomic update of the write index surely - is this guaranteed?
- learc83 8y agoYes most lock free thread safe paterns require the use of some atomic instructions. In the context of embeded real time systems you are generally targeting specific hardware, so you can make gurantees about the atomicity of specific instructions.
- tlb 8y agoThe requirement is generally that it can be written in a single instruction. This can fail on 32-bit processors like the x86 if you use a 64-bit index. For embedded work, it can fail on 8-bit processors if you use a 16-bit index. You can check, in C++, whether operations on a type are a single instruction. This might be true on an x84_64 and false on an x86: std::atomic<int64_t>::is_lock_free()