Guest User

Untitled

a guest
Oct 4th, 2011
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.25 KB | None | 0 0
  1. // compile with g++ "pushpull.cpp -o pp -lzmq -lboost_thread"
  2. #include <stdlib.h>
  3.  
  4. #include <iostream>
  5.  
  6. #include <boost/format.hpp>
  7. #include <boost/thread.hpp>
  8. #include <boost/thread/thread.hpp>
  9. #include <boost/thread/mutex.hpp>
  10.  
  11. #include <zmq.hpp>
  12.  
  13. using namespace std;
  14. using namespace boost;
  15.  
  16. class Counter {
  17.     mutex _mutex;
  18.     condition_variable waiting;
  19.     unsigned int count;
  20. public:
  21.     Counter()
  22.     {  
  23.         count = 0;
  24.     }
  25.     void increment()
  26.     {
  27.         mutex::scoped_lock lock(_mutex);
  28.         count ++;
  29.         waiting.notify_all();
  30.     }
  31.     void wait(unsigned int c=0)
  32.     {
  33.         mutex::scoped_lock lock(_mutex);
  34.         if (c==0) {
  35.             c = count + 1;
  36.         }
  37.         while (count < c) {
  38.             waiting.wait(lock);
  39.         }
  40.     }
  41. };
  42.  
  43.  
  44.  
  45. void sleeper_thread(zmq::context_t& zmq_ctx,
  46.                     Counter& counter,
  47.                     barrier& ready_barrier,
  48.                     int sleep_time,
  49.                     int thread_nr)
  50. {
  51.     zmq::socket_t puller(zmq_ctx, ZMQ_PULL);
  52.     uint64_t hwm = 1;
  53.     puller.setsockopt(ZMQ_HWM, &hwm, sizeof(hwm));
  54.  
  55.     string url = (format("inproc://puller.%i") % thread_nr).str();
  56.     puller.bind(url.c_str());
  57.     cout << "bound to "<<url<<endl;
  58.     ready_barrier.wait();
  59.  
  60.     while(true) {
  61.         sleep(sleep_time);
  62.         zmq::message_t msg;
  63.         puller.recv(&msg);
  64.         cout << "thread "<<thread_nr<<" got message\n";
  65.         counter.increment();
  66.     }
  67. }
  68.  
  69.  
  70.  
  71. int main(void)
  72. {
  73.     zmq::context_t zmq_ctx(1);
  74.     zmq::socket_t pusher(zmq_ctx, ZMQ_PUSH);
  75.  
  76.     uint64_t hwm = 1;
  77.     pusher.setsockopt(ZMQ_HWM, &hwm, sizeof(hwm));
  78.  
  79.     barrier ready_barrier(3);
  80.     Counter counter;
  81.  
  82.     thread fast_worker(sleeper_thread, ref(zmq_ctx),
  83.                        ref(counter), ref(ready_barrier),
  84.                        0, 0);
  85.     thread slow_worker(sleeper_thread, ref(zmq_ctx),
  86.                        ref(counter), ref(ready_barrier),
  87.                        1, 1);
  88.     ready_barrier.wait();
  89.  
  90.     pusher.connect("inproc://puller.0");
  91.     pusher.connect("inproc://puller.1");
  92.  
  93.     for (int i=0; i<20; i++) {
  94.         zmq::message_t msg(0);
  95.         pusher.send(msg);
  96.     }
  97.  
  98.     counter.wait(20);
  99.     return 0;
  100. }
  101.  
  102.  
  103.  
Advertisement
Add Comment
Please, Sign In to add comment