There are things to watch out for when implementing Spin buffers. For instance, the reader and writer on the Spin buffer must be called all the time; otherwise, you could run into a situation where exiting items in the buffer cannot be retrieved by the reader. This happens when writers have nothing to write; if put is not called, the items in the current write buffer can never be read by readers. One way around this is to call put with null, which forces the write buffer to advance to the next buffer, thereby letting the reader thread access the items in the buffer.
Spin buffers are also sensitive to impedance mismatch, which means slow readers slow down the writer and visa versa. The performance peaks when reads/writes are called at matching frequency.
The current implementation works when there is only one reader and one writer. If multiple readers and writers are needed, the put method must be synchronized; similarly, if there are multiple writer threads, the put method must be synchronized. However, the code for reading/writing need not be synchronized.
I did the comparative performance analysis on these three implementations:
- Ring Buffer.
- Java ConcurrentLinkedQueue (CLQ) (based on "Simple, Fast, and Practical Non-Blocking and Blocking Concurrent Queue Algorithms," by Maged M. Michael and Michael L. Scott; www .research.ibm.com/people/m/michael/podc-1996.pdf).
- Spin Buffer.
I conducted the performance tests on a hyperthreaded 3.0-GHz Pentium machine with 1 MB of RAM. I ran the tests multiple times for a minimum period of one hour on each of the different algorithm implementations. Table 1 presents the results. (In Table 1, I use the term "bandwidth" to describe how fast a buffer enables the data transfer from producer to consumer. The higher the bandwidth of the shared buffer implementation, the higher the number of items that can be transferred throughand the better the overall performance of the applications.) The performance test code is available www.ddj.com/code/.
|Implementation||Ring buffer||CLQ||Spin buffer|
Spin buffers are simple to implement, expose a straightforward API, and don't require special hardware or advanced processors. In spite of their shortcomings, the performance gains that Spin buffers deliver make an ideal choice for a wide range of applications such as game server engines, graphics, networking, and memory managers, among other high-performance applications.