Live data from Hacker News

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

blog.jaysmito.dev

11–20 of 42 posts

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

#11
post #4

Once you use atomic cmpxchg you've lost a great deal of scalability because it implies a retry loop (internal or by the user) The last thing you want is all of the threads failing to cmpxchg (spuriously or otherwise ) spinning on a shared cacheline Real world alternatives show atomic xchg only solutions scale to hundreds of threads.

Agreed. But you make it sound like the worst case is necessarily fatal. It depends on the use-case. The workable cmpxchg algorithms will make progress on at least one core each round. In a push or pop operation one of the cmpxchg must have succeeded for another to fail. The atomic xchg algorithms that I know of have other undesirable pathologies (e.g. a suspended producer can stall the consumer).

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

#12
post #10
post #8

Earlier quoted context omitted.

a C++ experienced programmer, I spoke recently to, told me that using new and delete is basically prohibited nowadays in C++ in favour of std::make_unique, std::make_shared etc.

For 95% of all code that is correct. std::make_* is easy to use and prevents a lot of mistakes, while having no loss of performance. That last 5% though you are doing weird things and so need to do something manually. (as the other poster said, make_unique is implemented with new) Of course the 5% is overall. Some projects never have anything that gets into that last 5%, while others it is more like 50% of the code c…

"For 95% of all code that is correct. std::make_* is easy to use and prevents a lot of mistakes, while having no loss of performance."

For 95% of code std::unique_ptr has no loss of performance. Perhaps. Just remember that it is not a zero-cost abstraction, the compiler won't always be able to entirely eliminate the overhead:

https://www.youtube.com/watch?v=rHIkrotSwcc

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

#14
post #8

Earlier quoted context omitted.

a C++ experienced programmer, I spoke recently to, told me that using new and delete is basically prohibited nowadays in C++ in favour of std::make_unique, std::make_shared etc.

In many application code bases no doubt. But how do you think make_unique and make_shared are implemented?

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

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

#15
post #10

Earlier quoted context omitted.

For 95% of all code that is correct. std::make_* is easy to use and prevents a lot of mistakes, while having no loss of performance. That last 5% though you are doing weird things and so need to do something manually. (as the other poster said, make_unique is implemented with new) Of course the 5% is overall. Some projects never have anything that gets into that last 5%, while others it is more like 50% of the code c…

"For 95% of all code that is correct. std::make_* is easy to use and prevents a lot of mistakes, while having no loss of performance." For 95% of code std::unique_ptr has no loss of performance. Perhaps. Just remember that it is not a zero-cost abstraction, the compiler won't always be able to entirely eliminate the overhead: https://www.youtube.com/watch?v=rHIkrotSwcc

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.

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

#16
post #14

Earlier quoted context omitted.

In many application code bases no doubt. But how do you think make_unique and make_shared are implemented?

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

I don't think adding unsafe will be possible - it will break too much existing code. We might be able to add something new that is only possible in an unsafe context, but there is too much existing code.

However I do expect a [[safe]] profile (or perhaps several, depending on which paper you read) that everyone is encourage to opt-in to. Likely combines with compiler warnings and static analysis to encourage that use. (Also syntax is still open for debate)

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

#17
> 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 complicated, so I avoided it for this implementation.

The reason using `compare_exchange_weak` is a better idea for lock-free algorithms is that, in most cases, you'll run it in a retry loop anyway. Since `compare_exchange_strong` is compiled to a retry loop, if you do a retry loop of `compare_exchange_strong` you basically have a loop in a loop. Using `compare_exchange_weak` makes things both simpler and more performant.

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

#19
post #16
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.

I don't think adding unsafe will be possible - it will break too much existing code. We might be able to add something new that is only possible in an unsafe context, but there is too much existing code. However I do expect a [[safe]] profile (or perhaps several, depending on which paper you read) that everyone is encourage to opt-in to. Likely combines with compiler warnings and static analysis to encourage that use…

Check the WG21 mailing proposals.

I was for Safe C++ paper, based on Circle experience, but it was shot down due to politics.

So we're left with the profiles camp actually delivering, followed by the remaining compilers caring to actually implement them, otherwise it will be static and dynamic analysis as usual.

Post reply on HN