News for hackers. Stuff that matters.
Re:A fast multi-producer, multi-consumer lock-free concurren... by vvanders on Sunday December 04, 2016 @08:43PM (#13102856)
There's a reason that there aren't many lock free implementations. If you don't get one from your CPU vendor then there's a high chance that it's not correct.
Re:A fast multi-producer, multi-consumer lock-free concurren... by DSMan195276 on Sunday December 04, 2016 @08:50PM (#13102886)
I would agree, but I'd add that it has been made a little bit easier to handle with the addition of `atomic` in C11 and C++11, since it means you can write atomic code without having to drop to inline assembly to ensure you use the right instructions to make it lock-free. That said, that's only one piece of the puzzle, and you really have to know what you're doing to ensure you write it correctly.

That said, after looking at this code the author appears to know what they're doing. I'd have to read it a lot closer to really make sure though.

Re:A fast multi-producer, multi-consumer lock-free concurren... by ape4 on Sunday December 04, 2016 @09:03PM (#13102925)
`atomic` may require a lock, depending on the CPU.
Re:A fast multi-producer, multi-consumer lock-free concurren... by DSMan195276 on Sunday December 04, 2016 @09:24PM (#13102989)
True. In that event though, there's little other options. Locking those variables individually like that is probably worse then other locking options though, so it is worth keeping in mind. but while it would be slower, it would still be just as 'correct' as using actual atomic variables.
Re:A fast multi-producer, multi-consumer lock-free concurren... by eloff on Monday December 05, 2016 @12:08AM (#13103501)
You wouldn't use this kind of queue on an architecture like that anyway, just use a lock and a regular queue (the lock is not avoidable anyway)
Re:A fast multi-producer, multi-consumer lock-free concurren... by flamedoge on Monday December 05, 2016 @04:46AM (#13104360)
As far as atomic 'types' are concerned, they rely on CPU hardware for atomicity. I wouldn't be surprised if generated code for non-supported width is done using lock. http://en.cppreference.com/w/cpp/atomic/atomic
Re:A fast multi-producer, multi-consumer lock-free concurren... by DSMan195276 on Monday December 05, 2016 @02:19PM (#13108368)
Don't get me wrong, I understand that atomic operations require CPU support. My point was that `atomic` allows you to get access to these operations in a more standard way for different platforms (And of course, it will fall back to generic locking in the event you use an unsupported width or operation - but that's arguably better then simply not working at all).
Re:A fast multi-producer, multi-consumer lock-free concurren... by gambiting on Monday December 05, 2016 @06:46AM (#13104799)
I've been to a C++11 presentation on multithreading recently, and x86 CPUs guarantee that all reads/writes on from std::atomic are indeed, atomic and they always happen in order they were called in. Code generated on ARM guarantees that even multi-byte reads/writes happen in one instruction, but there's no guarantee on the order.
Re:A fast multi-producer, multi-consumer lock-free concurren... by gpderetta on Monday December 05, 2016 @07:05AM (#13104871)
> x86 CPUs guarantee that all reads/writes on from std::atomic are indeed, atomic

... for sizeof(T) <= 64 bits on amd64.

128 bit is technically supported (thanks to the availability DCAS) but stores and especially loads are significantly more expensive (as there are no native atomic 128 bit load and store), even in relaxed modes. Also, as even loads perform write cycles, they can't be used on read-only memory.

Anything larger uses, IIRC, a spinlock pool and is not lock-free.

/pedantic

Re:A fast multi-producer, multi-consumer lock-free concurren... by wfunction on Sunday December 04, 2016 @08:45PM (#13102869)
Are you saying this one isn't correct? Confused what point you're trying to make.
Re:A fast multi-producer, multi-consumer lock-free concurren... by vvanders on Sunday December 04, 2016 @08:55PM (#13102901)
The atomic compare and swap is only one part of getting it right. You also have to deal with memory and instruction fences.

Certain architectures have different semantics when it comes to that ordering so something that works on x86 for instance might explode on PPC or ARM. If you want to verify that the algorithms are correct you basically have to do static analysis and the only way you'll do that is if you have access to the microcode and register models(I.E. you make CPUs).

[edit]

To put this into practical terms I spent some time working with a popular game engine that used a lockfree queue at it's core rendering path. It wasn't until 2 or 3 titles had shipped on this engine that it was discovered that there was a bug in the lockfree algo. We're talking trillions of operations under many different threading and context switching loads and at least 3 different CPU architectures.

Re:A fast multi-producer, multi-consumer lock-free concurren... by trungaczne on Sunday December 04, 2016 @09:32PM (#13103013)
I don't get the point you're trying to make. Aren't std::memory_order's guarantees supposed to work with all support archs?

The code in the OP does use memory fences. Are you implying that their implementation are incorrect?

Re:A fast multi-producer, multi-consumer lock-free concurren... by vvanders on Sunday December 04, 2016 @10:12PM (#13103133)
I'm not implying their implementation is incorrect. Just that these types of things are very easy to get wrong and when you do it's usually the type of bugs that take months to track down after you've eliminated every other subsystem involved.

Generally if there's not a huge organization putting their reputation(and $$$) on the line there is going to be bugs.

Most of the time if you're going lockfree for performance reasons there's usually much large gains to be found in your cache usage or overall architecture.

Re:A fast multi-producer, multi-consumer lock-free concurren... by sillysaurus3 on Sunday December 04, 2016 @10:18PM (#13103158)
Generally if there's not a huge organization putting their reputation(and $$$) on the line there is going to be bugs.

This argument applies to any hard problem, so it doesn't seem valid. Whether there's an important bug in a project depends on someone's skill and on how much time they've dedicated to it, and it's hard to know how skilled or dedicated someone is.

Re:A fast multi-producer, multi-consumer lock-free concurren... by lmm on Monday December 05, 2016 @06:56AM (#13104832)
When the prior against any particular implementation being correct is so high, I think it's correct to not trust any new implementation without strong evidence that it is correct, even if one is not aware of any specific issues. Personally I wouldn't adopt a new lock-free structure implementation without at least one of established backing or a formal proof of correctness.
Re:A fast multi-producer, multi-consumer lock-free concurren... by vvanders on Monday December 05, 2016 @10:11AM (#13105772)
Yeah, the more I think about it the parallels with hand-rolled crypto are pretty strong, although if you get crypto wrong you don't just crash.
Re:A fast multi-producer, multi-consumer lock-free concurren... by marcosdumay on Monday December 05, 2016 @11:37AM (#13106625)
You hit it, but it's not a side point.

The entire problem of rolling your own security code is that you will never know if you messed it up.

For functional code this is only an issue with silent corrupted data.

Re:A fast multi-producer, multi-consumer lock-free concurren... by je42 on Sunday December 04, 2016 @11:04PM (#13103293)
this is less about "skill" but about the awareness how the different CPUs are implemented and where the algorithm is not behaving correctly in conjunction with the CPU spec.

In addition the error class is a mean one: doesn't happen often statistically and difficult to reproduce and as such can be very expensive to track down.

Re:A fast multi-producer, multi-consumer lock-free concurren... by sillysaurus3 on Sunday December 04, 2016 @11:13PM (#13103320)
The specs are quite clear about memory fences. Just because something has a failure mode that's hard to detect doesn't mean that luck has anything to do with implementing it correctly. And if luck isn't a factor, then that leaves skill and dedication.
Re:A fast multi-producer, multi-consumer lock-free concurren... by vvanders on Monday December 05, 2016 @12:13AM (#13103519)
Specs/hw can have bugs too and he never said anything about luck.

I have no issue with hard problems but the accountability for concurrency issues is gnarly. I've had driver issues look like concurrency bugs and concurrency bugs look like driver issues. If you feel the need to take on concurrency you better have the schedule budget for it or be willing to throw it away.

Re:A fast multi-producer, multi-consumer lock-free concurren... by TeMPOraL on Sunday December 04, 2016 @11:03PM (#13103292)
Assuming I don't have access to CPU specs, how could I debug constructs like this queue? Should I set aside a machine and have it pound the queue with random data for the next six months, alerting me each time the error rate rises above cosmic ray threshold?
Re:A fast multi-producer, multi-consumer lock-free concurren... by vvanders on Monday December 05, 2016 @12:18AM (#13103535)
> a machine

In the absence of formal proof think 50+ machines. You basically setup a lab to run 24/7 and a/b with a hope that you repro.

Same technique works for really gnarly intermittent driver bugs.

It's still no guarantee but at least you get in the same range assuming the execution profile is diverse and robust.

Re:A fast multi-producer, multi-consumer lock-free concurren... by panic on Sunday December 04, 2016 @09:26PM (#13102994)
How was the bug discovered? Sounds like a fun story.
Re:A fast multi-producer, multi-consumer lock-free concurren... by jjawssd on Sunday December 04, 2016 @09:26PM (#13102995)
What was the solution?
Re:A fast multi-producer, multi-consumer lock-free concurren... by vvanders on Sunday December 04, 2016 @09:31PM (#13103009)
More fences :) (and the overhead that came with them)
Re:A fast multi-producer, multi-consumer lock-free concurren... by jdright on Sunday December 04, 2016 @10:00PM (#13103092)
Can you cite the name of the tech or any of the games?
Re:A fast multi-producer, multi-consumer lock-free concurren... by kitsuac on Sunday December 04, 2016 @11:35PM (#13103382)
Not true at all - the core atomic operations are very well understood by now and are supported on any modern CPU.
Re:A fast multi-producer, multi-consumer lock-free concurren... by dbaupp on Monday December 05, 2016 @12:53AM (#13103648)
This is a little optimistic: being able to tractibly reason about atomicity in weak memory models like C11 is still at the cutting edge of CS research. It is really hard to prove nontrivial data structures are correct.