Guest User

Untitled

a guest
Apr 23rd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. class B
  2. {
  3. public:
  4. void operator()(){run();}
  5. void run();
  6. void shutdown();
  7. ~B();
  8. B();
  9. boost::thread *thr;
  10. bool shutdown_requested;
  11. };
  12.  
  13. void B::shutdown()
  14. {
  15. shutdown_requested = true;
  16.  
  17. if (thr != NULL)
  18. {
  19. thr->interrupt();
  20. thr->join(); // deadlock occurs here!
  21. delete thr;
  22. thr = NULL;
  23. }
  24. }
  25.  
  26. B::~B()
  27. {
  28. shutdown();
  29. }
  30.  
  31. B::B()
  32. {
  33. thr = new boost::thread(boost::ref(*this));
  34. }
  35.  
  36. void B::run()
  37. {
  38. while (!shutdown_requested)
  39. {
  40. boost::xtime xt;
  41. boost::xtime_get(&xt, boost::TIME_UTC);
  42. xt.sec += 30;
  43. boost::this_thread::sleep(xt);
  44. }
  45. }
  46.  
  47. int main()
  48. {
  49. B *b = new B;
  50.  
  51. Sleep(5000);
  52. printf("deleting n");fflush(stdout);
  53. // b->shutdown();
  54. delete b;
  55. printf("donen");fflush(stdout);
  56.  
  57. return 0;
  58. }
  59.  
  60. int main()
  61. {
  62. B *b = new B;
  63.  
  64. Sleep(5000);
  65. printf("deleting n");fflush(stdout);
  66. b->shutdown();
  67. delete b;
  68. printf("donen");fflush(stdout);
  69.  
  70. return 0;
  71. }
Add Comment
Please, Sign In to add comment