Hacker News new | ask | show | jobs
by bob1029 1301 days ago
The best pattern I've found for inter-thread communication involves CAS for a ring buffer slot on the producer (you can skip CAS if only 1 producer thread), with either busy wait or yield/sleep on the consumer(s). If you keep your sync primitive separated between consumer & producer, the worst bits of contention can be avoided. CAS is very fast when un-contended or lightly-contended. Busy waiting can be used for latency-sensitive applications where you need threads coordinating with latency bounds measured in microseconds. yield/sleep can be used to play more fairly with other processes and power, but then you are at mercy of the OS timeslice size.
2 comments

One other great thing about the ring buffer is built-in sane backpressure. I think the article is good, but too quickly dismissive of backpressure as a tool for system health.
On x86, you're quite a bit better off using fetch-and-add instead of CAS.