Advertisement
Guest User

Untitled

a guest
Feb 18th, 2011
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.02 KB | None | 0 0
  1. // double_buffer.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6.  
  7. #include <boost/thread/condition.hpp>
  8. #include <boost/thread/mutex.hpp>
  9. #include <boost/thread/thread.hpp>
  10.  
  11. #include "binary_semaphore.hpp"
  12.  
  13. const int limit = 10;
  14.  
  15. typedef boost::mutex::scoped_lock lock;
  16. typedef std::vector< int > buffer_t;
  17.  
  18. // two buffer from which either a producer or consumer will work on
  19. buffer_t buffer1;
  20. buffer_t buffer2;
  21.  
  22. buffer_t* producer = &buffer1;
  23. buffer_t* consumer = &buffer2;
  24.  
  25. int num_produced_items = 0;
  26. int num_consumed_items = 0;
  27.  
  28. // we need two semaphores to block and unblock both threads appropriately
  29. binary_semaphore buffer_empty( true ), buffer_ready( false );
  30.  
  31.  
  32. // there are three threads in total to allow the user to exit the program just by hitting a key.
  33. boost::mutex stop_guard;
  34. bool stop = false;
  35.  
  36. bool finish()   { lock lk(stop_guard); return stop; }
  37. void set_stop() { lock lk(stop_guard); stop = true; }
  38.  
  39. void produce()
  40. {
  41.     while( true )
  42.     {
  43.         // simulate producing
  44.         boost::this_thread::sleep( boost::posix_time::millisec( 500 ));
  45.         producer->push_back( 0 );
  46.         std::cout << "producing from "
  47.                   << (( producer == &buffer1 ) ? "1" : "2" )
  48.                   << " : " << producer->size()
  49.                   << std::endl;
  50.  
  51.         ++num_produced_items;
  52.  
  53.         if( producer->size() == limit || finish() == true )
  54.         {
  55.             buffer_empty.wait();
  56.  
  57.             // swap pointers
  58.             buffer_t* temp = producer;
  59.             producer = consumer;
  60.             consumer = temp;
  61.  
  62.             buffer_ready.signal();
  63.         }
  64.  
  65.         if( finish() == true )
  66.         {
  67.             break;
  68.         }
  69.     }
  70. }
  71.  
  72. void consume()
  73. {
  74.     bool exit = false;
  75.  
  76.     while( exit == false )
  77.     {
  78.         buffer_ready.wait();
  79.  
  80.         // simulate consuming
  81.         std::size_t num = consumer->size();
  82.  
  83.         if( num < limit )
  84.         {
  85.             exit = true;
  86.         }
  87.  
  88.         for( std::size_t i = 0; i < num; ++i )
  89.         {
  90.             int item = consumer->back();
  91.             consumer->pop_back();
  92.  
  93.             boost::this_thread::sleep( boost::posix_time::millisec( 1500 ));
  94.  
  95.             std::cout << "consuming from "
  96.                       << (( consumer == &buffer1 ) ? "1" : "2" )
  97.                       << ": "
  98.                       << consumer->size()
  99.                       << std::endl;
  100.  
  101.             ++num_consumed_items;
  102.         }
  103.  
  104.         buffer_empty.signal();
  105.     }
  106. }
  107.  
  108. int main( int argc, char* argv[] )
  109. {
  110.     buffer1.reserve( limit );
  111.     buffer2.reserve( limit );
  112.  
  113.     boost::thread thrd1( &produce );
  114.     boost::thread thrd2( &consume );
  115.  
  116.     std::cout << "Press any key to finish program..." << std::endl;
  117.  
  118.     getchar();
  119.  
  120.     set_stop();
  121.  
  122.     thrd1.join();
  123.     thrd2.join();
  124.  
  125.     std::cout << "produced items: " << num_produced_items << std::endl;
  126.     std::cout << "consumed items: " << num_consumed_items << std::endl;
  127.  
  128.     return 0;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement