Poco::Thread 生产者消费者Demo

procon.cc

#include <cstdlib>
#include <iostream>
#include <vector>

#include <Poco/Thread.h>
#include <Poco/Runnable.h>
#include <Poco/Mutex.h>

using Poco::Runnable;
using Poco::Thread;
using Poco::Mutex;
using Poco::FastMutex;

class SharedResourc {
   public:
      void produce()
      {
         _fm.lock();
         _vec.push_back(getRandNum());
         std::cout << "+After Produce: ";
         printVec();
         _fm.unlock();
      }
      void consume()
      {
         _fm.lock();
         if(!_vec.empty())
            _vec.pop_back();
         std::cout << "-After Consume: ";
         printVec();
         _fm.unlock();
      }
   private:
      FastMutex _fm;
      std::vector<int> _vec;
      void printVec()
      {
         for(int i=0; i<_vec.size(); ++i)
            std::cout << _vec[i] << " ";
         std::cout << std::endl;
      }
      int getRandNum()
      {
         return (int) (rand() % 80 + 10);
      }
};

class ProducerThread: public Runnable {
   public:
      ProducerThread(SharedResourc& sr):
         _sr(sr)
      {
      }

      virtual void run()
      {
         for (int i=0; i<17; ++i) {
            _sr.produce();
            Thread::sleep(400);
         }
      }
   private:
      SharedResourc& _sr;
};

class ConsumerThread: public Runnable {
   public:
      ConsumerThread(SharedResourc& sr):
         _sr(sr)
      {
      }

      virtual void run()
      {
         for (int i=0; i<25; ++i) {
            _sr.consume();
            Thread::sleep(700);
         }
      }
   private:
      SharedResourc& _sr;
};

// ===  FUNCTION  ======================================================================
//         Name:  main
//  Description:  main function
// =====================================================================================
   int
main ( int argc, char *argv[] )
{
   SharedResourc sr;
   ProducerThread pt(sr);
   ConsumerThread ct(sr);

   Thread th1, th2;
   th1.start(pt);
   th2.start(ct);

   th1.join();
   th2.join();

   return EXIT_SUCCESS;
}     // ----------  end of function main  ----------

结果:

[root@slayer poco]# g++ procon.cc -lPocoFoundation
[root@slayer poco]# ./a.out
+After Produce: 33
-After Consume:
+After Produce: 16
-After Consume:
+After Produce: 67
+After Produce: 67 45
-After Consume: 67
+After Produce: 67 43
+After Produce: 67 43 25
-After Consume: 67 43
+After Produce: 67 43 36
-After Consume: 67 43
+After Produce: 67 43 22
+After Produce: 67 43 22 19
-After Consume: 67 43 22
+After Produce: 67 43 22 71
+After Produce: 67 43 22 71 52
-After Consume: 67 43 22 71
+After Produce: 67 43 22 71 37
+After Produce: 67 43 22 71 37 60
-After Consume: 67 43 22 71 37
+After Produce: 67 43 22 71 37 69
-After Consume: 67 43 22 71 37
+After Produce: 67 43 22 71 37 13
+After Produce: 67 43 22 71 37 13 16
-After Consume: 67 43 22 71 37 13
+After Produce: 67 43 22 71 37 13 70
-After Consume: 67 43 22 71 37 13
-After Consume: 67 43 22 71 37
-After Consume: 67 43 22 71
-After Consume: 67 43 22
-After Consume: 67 43
-After Consume: 67
-After Consume:
-After Consume:
-After Consume:
-After Consume:
-After Consume:
-After Consume:
-After Consume:
-After Consume:
-After Consume:

 

posted @ 2013-02-18 18:34  Leo Forest  阅读(540)  评论(0编辑  收藏  举报