Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

Exchanger Class Reference

A synchronization point at which two threads can exchange objects. Each thread presents some object on entry to the exchange method, and receives the object presented by the other thread on return. More...


Detailed Description

A synchronization point at which two threads can exchange objects. Each thread presents some object on entry to the exchange method, and receives the object presented by the other thread on return.

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();
 }
 


The documentation for this class was generated from the following file:
Generated on Mon Nov 14 10:59:52 2005 for Mango by  doxygen 1.4.0