Ring Buffer

 [Previous Chapter]  [Previous Page]  [Contents]  [Next Page]  [Next Chapter]

template<class Token>
class RingBuffer {
      // constructors and destructors
      RingBuffer(unsigned int size);

      // mutators
      void add(Token& token);
      Token get();

      std::vector<Token> buffer;
      unsigned int filled; // # of objects in the buffer
      unsigned int in; // next token is stored here
      unsigned int out; // next token is taken from here
      pthread_mutex_t mutex;
      pthread_cond_t nonEmpty;
      pthread_cond_t nonFull;
}; // class RingBuffer

*Ring buffers are frequently used for concurrent producer/consumer relationships.
*mutex is used to assure mutual exclusion if the data members are accessed.
*nonEmpty and nonFull are used to signal that a stack is no longer empty (for waiting consumers) or that a stack is no longer full (for waiting producers), respectively.

 [Previous Chapter]  [Previous Page]  [Contents]  [Next Page]  [Next Chapter]
Copyright © 2001, 2002 Andreas Borchert, converted to HTML on February 21, 2002