fork download
  1. #include <iostream>
  2. #include <thread>
  3. #include <mutex>
  4. #include <map>
  5. #include <string>
  6.  
  7. std::map<std::string, std::string> g_pages;
  8. std::mutex g_pages_mutex;
  9.  
  10. void save_page(const std::string &url)
  11. {
  12. std::string result = "fake content";
  13.  
  14. g_pages_mutex.lock();
  15. try{
  16. g_pages[url] = result;
  17. if( url.find("thrd2", 0) != std::string::npos )
  18. throw "I'm naipnulosya";
  19. }
  20. catch(const char * msg){
  21. g_pages[url] = msg;
  22. }
  23. g_pages_mutex.unlock();
  24. }
  25.  
  26. int main()
  27. {
  28. std::thread t1(save_page, "http://thrd1URL");
  29. std::thread t2(save_page, "http://thrd2URL");
  30. t1.join();
  31. t2.join();
  32.  
  33. g_pages_mutex.lock();
  34. for (const auto &pair : g_pages) {
  35. std::cout << pair.first << " => " << pair.second << '\n';
  36. }
  37. g_pages_mutex.unlock();
  38. return 0;
  39. }
Success #stdin #stdout 0s 20872KB
stdin
Standard input is empty
stdout
http://thrd1URL => fake content
http://thrd2URL => I'm naipnulosya