Implementing a Spinlock in C++
talkinghightech.com
talkinghightech.com
https://www.realworldtech.com/forum/?threadid=189711&curpost...
Now, let's say several threads have to write their results into a common buffer. Ideally, they would first write into individual buffers which you can safely sum after a synchronization point. However, depending on the architecture, this might not always be possible and a spinlock might be the only practical solution. Note again that audio threads typically run with real-time priority and should not be preempted, so the major issues with spinlocks - that Linus correctly points out! - don't apply, AFAICT.
One example for audio software that uses spinlocks is "Supernova", a multi-threaded reimplementation of the "scsynth" audio server. Here the author describes the locking strategy: https://www.complang.tuwien.ac.at/Diplomarbeiten/blechmann11... (section 4.3)
Basically your standard CAS loop with _mm_pause() to yield.