163 points · by signa11 · 4y ago · 89 comments
The mpmc variant looks non-serialising (threads push multiple things before another manages to push one) so it's not a queue, and it has a cas loop in it so lock-free is a bit of a stretch.
Many lock-free structures use CAS loops. It would not be lock-free if a consumer pre-empted at the wrong time could prevent other consumers to make progress, but I don't think this is the case here.
To be fair, if that's your standard, there are virtually no lock-free algorithms. The multi-consumer has a wait-free retry loop. That's pretty much as good as it gets.
And the SPSC case is lock-free under even the strictest definitions, though of course that's not particularly difficult to achieve.
In rough terms, every push or pop from the queue also causes head or tail to be written, thus dirtying a line of cache. That’s the bit I misunderstood.
For the multiple-consumer variant, however, consistency between head and tail is required so packing them in the same atomic is the simplest solution.
When I transitioned from trading to tech, I realized that HFT data structures have limited usefulness when you are not also doing HFT. You can do a lot more things lock-free when you don't have to worry about adverse scheduling.
By the way, there is no easy way to fix this with an MPMC ring buffer. You have to accept some chance of adverse behavior. There may be something complicated you can do, but it's easier to just split into multiple rings.
This is why newer versions of LMAX offer additional wait strategies beyond BusyWait. The one I prefer to use, unless latency is absolutely insanely critical, is the YieldingWait strategy.
In fact the other side value is normally cached in a local memory location so to amortize the cost of roundtrips over multiple reads and writes.
Cmon AMD/Intel, give us hardware queues already :)
These do a bit more though, as they also allow batch stealing operations.
[1] https://github.com/golang/go/blob/master/src/runtime/proc.go...
[2] https://tokio.rs/blog/2019-10-scheduler#a-better-run-queue
And is the acquire ordering on push/pop and release on commit the right thing?
Also, my C skills are lacking but I recall that C's support for atomic data structures is a higher-level abstraction that could very well use locks under the hood.
C's support for atomics even provides a atomic_is_lock_free function which is used to check whether a data structure is lock-free or not.
https://en.cppreference.com/w/c/atomic/atomic_is_lock_free
Using ints with C's atomics does not ensure the atomic data type is lock-free. C even provides a macro to verify whether the primitive data type has lock-free access or not.
https://en.cppreference.com/w/c/atomic/ATOMIC_LOCK_FREE_cons...
Using C's atomic in this context to claim lock-free status sounds a lot like hand-waving over the problem domain to pretend locks aren't there. It's a cool read, but the underlying claim doesn't really hold.
Don't know if there is data to back that up, though.
So basically, writing a shared line is expensive. Atomic reads are free, as are atomic writes to a cache line that no one else is using.
- Less memory - a pthread mutex is around 40 bytes, compared to 4 bytes for this queue.
- No waiting - in some cases you don't want to wait, just do a try-push and if you can't, do some other work with the thread and then try again. Eventually you will have to either wait or discard the data, depending on your situation.
- No syscalls - mutexes weren't always as light as they are now, older implementations would always do a syscall and context switch to take or release a mutex.
“The single producer and single consumer didn’t require locks nor atomic accesses to the storage array since the queue guaranteed that accesses at the specified index were not concurrent. However, this is not the case with multiple-consumers. Consumers race when popping. The loser’s access might occur after the winner’s commit, making its access concurrent with the producer. Both producer and consumers must account for this.”
C puts atomic on the type, not the operation. That's conservative and occasionally very annoying (can't use normal stores in some parts of the program and atomic in others, thanks to aliasing rules) but doesn't hurt this particular application.
Maybe TFA is for embedded use?
You can use the entire buffer and not waste one slot by doing:
push: buf[writeidx % bufsize] = x;
writeidx++;
pop: x = buf[readidx % bufsize];
readidx++;
Unsigned integers, you get size by doing: writeidx - readidx; Then you check size==0 or size==bufsize to detect empty/full.
He has a companion post about a "magic" ring buffer that uses a virtual memory mapping to make a ring buffer where you can always address the entire sizes as contiguous memory, but I think it has a bit of minor bit rot in the linked implementations. https://fgiesen.wordpress.com/2012/07/21/the-magic-ring-buff...
Keep in mind that one of the reasons behind the arbitrary "power of two" requirement is that the method proposed by the author abuses a single atomic integer to hold two shorts representing the indices for the head and tail elements.
I don't think it makes much sense to talk about overhead given this method requires doing a bunch of bitmasks to operate over the high and low short.
Larger CPUs can do many such operations during every clock cycle and even in very small CPUs they are usually single-cycle operations.
Depending on the CPU, integer divisions are 10 to 100 times slower, so in a completely different range of overhead.
Modular arithmetic makes it work. Try a few examples close to UINT_MAX if you want to convince yourself.
Using two distinct 16bit _Atomic variables would have been an improvement, but I guess having both pointers in the same cacheline is going through be the bottleneck anyway.
Imagine I have a Gui where the user presses a button, upon which a message is sent to a queue, upon which a consumer reads it and performs some long-running task. Why should that queue be bounded?
If they are just batch background tasks (i.e. there is no expectation of seeing some immediate UI update as a response), then having a larger queue might be fine. There's not really that much reason to put a (small) limit on say the number of files to be downloaded in a queue. And having a really large queue bound is not that dissimilar from having an arbitrarily large queue (limited be memory of course), in terms of buffer bloat and added latency.
"Larger" is not good enough; you really want an unbounded queue (unbounded in theory of course), because otherwise the GUI thread might block at some point. Unless you want to spend a lot of effort on exception handling (at which point unbounded is still the best choice, see below).
Also, unnecessary restrictions in your program might cause problems in the future when your software is ported to a larger architecture, or when demands change.
Bounds let you have a `try_push` function that can obviously fail, whereas with unbounded you can only really know it failed because of OOM, or your producer thread gets blocked.
When `try_push` failure happens you know something strange is happening in your code.
Unbounded is preferable because that's how we do everything else, and best prepares for porting to larger architectures. Do your strings have a maximum size, for example?
The size of the queue can be read from a config file, and is tuned to the capability of the machine.
For (;;) … While (condition…) …
Why not use while in both situations, so while(1)… instead of for(;;) ?
There’s no other occasion I can think of where an expression becomes vacuously true by being omitted. If it were conceptually coherent you’d write for(;true;). I guess maybe the reason they made C accept for(;;) but not while() is because while() might confuse a compiler by looking like a function call.
Syntax wise, it doesn't really matter. "for" just happens to be the keyword that allows omitting the condition. "While" effectively means "during which". "For" on the other hand has a significantly more flexible meaning; it seems reasonable to me that the for keyword would be more flexible. Syntax wise, the most explicit thing to have in a language would be a "forever" keyword of some sort.
Because some compilers warn that the condition is constant and suggest you rewrite it as for (;;)
for (;;) is less typing.
for (;;) has always been the idiomatic way to do an infinite loop.
FWIW https://github.com/robohack/ucb-csrg-bsd contains both variants.
Not a problem now.
They lack the code to remove a tautological test.
But approximately all compilers these days are optimising compilers, so don’t worry about it.