Sample Usage: Here are the highlights of a class that uses an Exchanger
to swap buffers between threads so that the thread filling the buffer gets a freshly emptied one when it needs it, handing off the filled one to the thread emptying the buffer.
Exchanger!(DataBuffer) exchanger = new Exchanger!(DataBuffer); DataBuffer initialEmptyBuffer = ... a made-up type DataBuffer initialFullBuffer = ... Thread t1 = new Thread( delegate int() { DataBuffer currentBuffer = initialEmptyBuffer; while (currentBuffer != null) { addToBuffer(currentBuffer); if (currentBuffer.full()) currentBuffer = exchanger.exchange(currentBuffer); } return 0; }); Thread t2 = new Thread( delegate int() { DataBuffer currentBuffer = initialFullBuffer; while (currentBuffer != null) { takeFromBuffer(currentBuffer); if (currentBuffer.empty()) currentBuffer = exchanger.exchange(currentBuffer); } return 0; }); t1.start(); t2.start(); }