Ring Buffer Demo

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

RingBufferDemo.C
typedef RingBuffer<int> Buffer;

extern "C" void* producer(void* arg) {
   Buffer* buffer = (Buffer*) arg;

   for (int i = 10; i >= 0; --i) {
      cout << "producer: " << i << endl;
      buffer->add(i);
   }
   return 0;
} // producer

extern "C" void* consumer(void* arg) {
   Buffer* buffer = (Buffer*) arg;

   int token;
   while (token = buffer->get()) {
      cout << "consumer: " << token << endl;
   }
   return 0;
} // consumer

int main() {
   RingBuffer<int> buffer(4);
   pthread_t prod;
   pthread_t cons;

   pthread_create(&prod, 0, producer, &buffer);
   pthread_create(&cons, 0, consumer, &buffer);

   void* resultp;
   pthread_join(prod, &resultp);
   pthread_join(cons, &resultp);
} // main

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