Live data from Hacker News

Building a Fast Lock-Free Queue in Modern C++ from Scratch

blog.jaysmito.dev

31–40 of 42 posts

Re: Building a Fast Lock-Free Queue in Modern C++ from Scratch

#31

How can I make a lock free queue without OS support, ESP32?

I'd ask your friendly neighborhood LLM for design advice, but off the top of my head, so long as `static_assert(std::atomic ::is_lock_free)` passes, you could build a lock-free SPSC queue with just plain ol' std::atomics and probably liberal use of `alignas()` calls

Most local LLMs I've used are not particularly great with the advice unfortunately, probably because I haven't spent thousands of dollars on new hardware recently

Re: Building a Fast Lock-Free Queue in Modern C++ from Scratch

#32

> NOTE: Throughout our implementation we strictly use compare_exchange_strong, but the C++ standard suggests that for some systems like ARM it mighe be a better idea to run a loop with compare_exchange_weak for better performance. The problem with that is a compare_exchange_weak call may fail spuriously, which essentially it can randomly fail even if everything is correct, thus it makes code code a bit more complicat…

This is overstated in value; on arm systems with LSE it's faster to use that even for weak operations than to use ll/sc. Even if you are limited to ll/sc the compiler may not put your cas-loop body into the ll/sc region as there's limitations on how many and what type of instructions are permitted there.

Sure, with LSE. But you have to target LSE and get a system that actually contains it, i.e. not the default compiler flags and not the original AWS Graviton.

Re: Building a Fast Lock-Free Queue in Modern C++ from Scratch

#33
post #15

Earlier quoted context omitted.

I watched that about 7 years ago when it first came out, and at an hour long I'm not about to watch it again... From what I can recall those are things that rarely are important. There is a cost, but in most real world code they only add a few nanoseconds - I have more important things to worry about.

As a firmware developer commonly operating on devices with 32MHz of cpu, i am offended sir.

Even at 32Mhz you typically have more important things to worry about. These nanoseconds are unlikely to add up - the overhead from the underlying new/delete is going to be much worse.

Re: Building a Fast Lock-Free Queue in Modern C++ from Scratch

#34
post #30
post #14

Earlier quoted context omitted.

With what will most likely become [[unsafe]] profile in C++29, assuming WG21 actually gets their profiles story right.

Classic C++, any issue is at most three years away from going away! There will definitely not be any other issues that are also three years from getting fixed when you reach that year though

Actually no, because you are missing the time between standard being ratified and actuality being widely available in all major compilers.

By the way, it is also classical Web standards, C, Vulkan, OpenCL, and everything else that has a similar process.

Ideally we should have gotten rid of C and C++, but I haven't yet found a better Typescript for C.

Re: Building a Fast Lock-Free Queue in Modern C++ from Scratch

#35
post #31

Earlier quoted context omitted.

I'd ask your friendly neighborhood LLM for design advice, but off the top of my head, so long as `static_assert(std::atomic ::is_lock_free)` passes, you could build a lock-free SPSC queue with just plain ol' std::atomics and probably liberal use of `alignas()` calls

Most local LLMs I've used are not particularly great with the advice unfortunately, probably because I haven't spent thousands of dollars on new hardware recently

Edited my comment for clarity. I meant "friendly local" as a euphemism, like "neighborhood pub".

Anyways. Ask Claude.

Re: Building a Fast Lock-Free Queue in Modern C++ from Scratch

#36
post #31

Earlier quoted context omitted.

Most local LLMs I've used are not particularly great with the advice unfortunately, probably because I haven't spent thousands of dollars on new hardware recently

Edited my comment for clarity. I meant "friendly local" as a euphemism, like "neighborhood pub". Anyways. Ask Claude.

Fair enough! It occurred to me after sending my comment that maybe you didn't mean it literally, but I'm easily confused, so clarity is helpful!

Re: Building a Fast Lock-Free Queue in Modern C++ from Scratch

#37
post #15

Earlier quoted context omitted.

I watched that about 7 years ago when it first came out, and at an hour long I'm not about to watch it again... From what I can recall those are things that rarely are important. There is a cost, but in most real world code they only add a few nanoseconds - I have more important things to worry about.

As a firmware developer commonly operating on devices with 32MHz of cpu, i am offended sir.

If your doing firmware you probably shouldn't be doing much memory allocation at runtime anyway to prevent memory fragmentation.

Re: Building a Fast Lock-Free Queue in Modern C++ from Scratch

#38
post #34
post #30

Earlier quoted context omitted.

Classic C++, any issue is at most three years away from going away! There will definitely not be any other issues that are also three years from getting fixed when you reach that year though

Actually no, because you are missing the time between standard being ratified and actuality being widely available in all major compilers. By the way, it is also classical Web standards, C, Vulkan, OpenCL, and everything else that has a similar process. Ideally we should have gotten rid of C and C++, but I haven't yet found a better Typescript for C.

Fair enough, three years might be more of an upper bound than a lower bound. And yeah, the issue of having to wait is common to standard-based processes, but I've never seen anything else based on a standard so frequently have people need to jump in and try to dissuade people that issues matter because of the rolling timeline so frequently, regularly, and for such a long time as C++. Obviously it would be unreasonable to expect it to become perfect and not need any more changes, but like, I was hearing people complain about various aspects of `unique_ptr` a decade ago, and it seems like we're not even past that yet.

Re: Building a Fast Lock-Free Queue in Modern C++ from Scratch

#39
post #7

Not trying to be critical, but there are a number of misspellings and grammatical issues and it was actually a breath of fresh air to be reminded while I was reading that a real human being wrote this. I feel a little inspired to turn off spell check for my own writing.

Strong "oll korrect" vibes.

Re: Building a Fast Lock-Free Queue in Modern C++ from Scratch

#40

> NOTE: Throughout our implementation we strictly use compare_exchange_strong, but the C++ standard suggests that for some systems like ARM it mighe be a better idea to run a loop with compare_exchange_weak for better performance. The problem with that is a compare_exchange_weak call may fail spuriously, which essentially it can randomly fail even if everything is correct, thus it makes code code a bit more complicat…

This is overstated in value; on arm systems with LSE it's faster to use that even for weak operations than to use ll/sc. Even if you are limited to ll/sc the compiler may not put your cas-loop body into the ll/sc region as there's limitations on how many and what type of instructions are permitted there.

On ARM systems with LSE both weak and strong compile to the same CAS instruction anyway. On LL/SC, using strong means CPU will attempt to retry the same exchange on spurious failure, which increases likelihood that another thread will update the value in the meantime, requiring an outer loop retry.

On any plaform which implements strong exchange with zero overhead over weak exchange, we can safely assume they compile to the same thing, and use weak. The only reason to use `strong` is if you have a one-shot CAS operation and you don't want to write a retry loop.

Post reply on HN