Advertisement
zhangsongcui

Delegate of C++

Apr 4th, 2011
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.16 KB | None | 0 0
  1. #include <iostream>
  2. #include <boost/thread.hpp>
  3. #include <boost/signals2.hpp>
  4. #include <boost/bind.hpp>
  5.  
  6. boost::mutex theMutex;
  7.  
  8. struct Obj
  9. {
  10.     void fn1()
  11.     {
  12.         boost::lock_guard<boost::mutex> lock(theMutex);
  13.         std::cout << (void *)this << "::fn1" << std::endl;
  14.     }
  15.     void fn2()
  16.     {
  17.         boost::lock_guard<boost::mutex> lock(theMutex);
  18.         std::cout << (void *)this << "::fn2" << std::endl;
  19.     }
  20.     void fn3()
  21.     {
  22.         boost::lock_guard<boost::mutex> lock(theMutex);
  23.         std::cout << (void *)this << "::fn3" << std::endl;
  24.     }
  25. };
  26.  
  27. struct Obj2
  28. {
  29.     void operator()()
  30.     {
  31.         boost::lock_guard<boost::mutex> lock(theMutex);
  32.         std::cout << (void *)this << ":Hello world" << std::endl;
  33.     }
  34. };
  35.  
  36. int main()
  37. {
  38.     Obj o1, o2;
  39.     Obj2 HW;
  40.     boost::signals2::signal<void(void)> sig1, sig2;
  41.     sig1.connect(HW);
  42.     sig1.connect(boost::bind(&Obj::fn1, &o1));
  43.     sig1.connect(boost::bind(&Obj::fn2, &o1));
  44.     sig1.connect(boost::bind(&Obj::fn3, &o2));
  45.     sig2.connect(boost::bind(&Obj::fn1, &o2));
  46.     sig2.connect(boost::bind(&Obj::fn2, &o2));
  47.     sig2.connect(boost::bind(&Obj::fn3, &o1));
  48.     sig2.connect(HW);
  49.     boost::thread t1(boost::ref(sig1)), t2(boost::ref(sig2));
  50.     t1.join();
  51.     t2.join();
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement