fork download
  1. #include <iostream>
  2. #include <thread>
  3. #include <mutex>
  4. #include <string>
  5.  
  6.  
  7. std::mutex g_pages_mutex;
  8.  
  9. class cButtonEmul{
  10. protected:
  11. bool bState;
  12. public:
  13. cButtonEmul(bool bState = true){
  14. cButtonEmul::bState = bState;
  15. }
  16. bool getState(){return bState;}
  17. bool setState(bool bState){
  18. cButtonEmul::bState = bState;
  19. }
  20. };
  21.  
  22. struct sThreadParam{
  23. std::string *url;
  24. cButtonEmul *btn;
  25. };
  26.  
  27.  
  28. void save_page(const sThreadParam &param)
  29. {
  30. if( param.btn )
  31. param.btn->setState(false);
  32. g_pages_mutex.lock();
  33. try
  34. {
  35. try
  36. {
  37. if( param.url ){
  38. if( param.url->find("thrd2", 0) != std::string::npos )
  39. throw "I'm naipnulosya";
  40. else
  41. if( param.url->find("thrd1", 0) != std::string::npos )
  42. throw 0;
  43. else
  44. *param.url = "fake content";
  45. }
  46. }
  47. catch(const char * msg){
  48. if( param.url )
  49. *param.url = msg;
  50. }
  51. }
  52. catch(...){
  53. g_pages_mutex.unlock();
  54. if( param.url )
  55. *param.url = "unhandled exception occured!";
  56. if( param.btn )
  57. param.btn->setState(true);
  58. return;//àâàð³éíèé âèõ³ä
  59. }
  60. g_pages_mutex.unlock();
  61. if( param.btn )
  62. param.btn->setState(true);
  63. }
  64.  
  65. int main()
  66. {
  67. cButtonEmul btn1(false);std::string sDownload1 = "http://thrd1URL";
  68. cButtonEmul btn2(false);std::string sDownload2 = "http://thrd2URL";
  69. cButtonEmul btn3(false);std::string sDownload3 = "http://thrd3URL";
  70. sThreadParam prm1 = {&sDownload1, &btn1};
  71. sThreadParam prm2 = {&sDownload2, &btn2};
  72. sThreadParam prm3 = {&sDownload3, &btn3};
  73. std::thread t1(save_page, prm1);
  74. std::thread t2(save_page, prm2);
  75. std::thread t3(save_page, prm3);
  76. t1.join();
  77. t2.join();
  78. t3.join();
  79.  
  80. g_pages_mutex.lock();
  81. std::cout<< sDownload1 <<" btn1 state : "<<std::string(btn1.getState() ? "enabled" : "disabled")<<std::endl;
  82. std::cout<< sDownload2 <<" btn2 state : "<<std::string(btn2.getState() ? "enabled" : "disabled")<<std::endl;
  83. std::cout<< sDownload3 <<" btn3 state : "<<std::string(btn3.getState() ? "enabled" : "disabled")<<std::endl;
  84. g_pages_mutex.unlock();
  85. return 0;
  86. }
  87.  
Success #stdin #stdout 0s 29064KB
stdin
Standard input is empty
stdout
unhandled exception occured! btn1 state : enabled
I'm naipnulosya btn2 state : enabled
fake content btn3 state : enabled