Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

TimeBomb.cpp

By: a guest on Mar 13th, 2012  |  syntax: C++  |  size: 1.33 KB  |  hits: 70  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <iostream>
  2. #include "TimeBomb.hpp"
  3. #include <boost/date_time/posix_time/posix_time.hpp>
  4.  
  5. using namespace std;
  6. using namespace boost;
  7. using namespace boost::python;
  8.  
  9. void ITimeBombCallbackWrapper::ReceiveExplosion()
  10. {
  11.         // Yeap, getting the GIL.
  12.         PyGILState_STATE gstate = PyGILState_Ensure();
  13.         this->get_override("ReceiveExplosion")();
  14.         PyGILState_Release(gstate);    
  15. }
  16.  
  17. TimeBomb::TimeBomb(ITimeBombCallback* luckyWinner)
  18. {
  19.         this->luckyWinner = luckyWinner;
  20.         backgroundThread = NULL;
  21. }
  22.  
  23. void TimeBomb::Go()
  24. {
  25.         backgroundThread = new boost::thread(boost::bind(&TimeBomb::InnerBody, this));
  26. }
  27.  
  28. void TimeBomb::InnerBody()
  29. {
  30.         cout << "Inside the inner body of the time bomb." << endl;
  31.  
  32.         // We'll just do some donuts in the parking lot here while we wait for 5 seconds
  33.         boost::this_thread::sleep(boost::posix_time::seconds(5));
  34.  
  35.         cout << "Five seconds are up.  Time to deliver payload." << endl;
  36.  
  37.         // Original callback that started this whole mess was firing off quite a few in a short duration,
  38.         // so that's why we repeat the call in such close succession.
  39.         // Indeed, the first call often survives.
  40.         for(int i = 0; i < 5; ++i)
  41.         {
  42.                 luckyWinner->ReceiveExplosion();
  43.         }
  44.  
  45.         cout << "Payload delivered." << endl;
  46. }
  47.  
  48. void TimeBomb::Join()
  49. {
  50.         if(backgroundThread != NULL)
  51.         {
  52.                 backgroundThread->join();
  53.         }
  54. }