// boundedbuf.h // This is a simple bounded buffer of ints intended to be used as a // communication channel between threads. It offers the extra feature // that the producer can explicitly "shutdown" the buffer to indicate // that no more ints will follow; the get procedure notices this and // returns 0 (once the buffer has drained to empty) rather than waiting for // more data to come. This is used as a demonstration of locks and condition // variables. -Max Hailperin 9/23/96 #include "synch.h" class BBuf{ public: BBuf(); void put(int); void shutdown(); // signals no more values will be put int get(int&); // returns 1 if a value was gotten, 0 if none is available // and none ever will be; the value goes into the argument private: enum {size=10}; int buf[size]; int count; int nextRead, nextWrite; // index in buf for next read/write int shut; // 0 = more data might come Lock *lock; // used for mutual exclusion (monitor style) Condition *data; // signalled when there is data added to an empty buf Condition *space; // signalled when space becomes available in a full buf };