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
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.