fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <mutex>
  4. #include <thread>
  5. #include <map>
  6.  
  7. std::map<std::string, std::string> map1 ( {{"apple","red"},{"lemon","yellow"}} );
  8. static auto const map_it_end = map1.end();
  9. std::mutex mtx1;
  10.  
  11. void func() {
  12. std::lock_guard<std::mutex> lock1(mtx1);
  13.  
  14. auto it1 = map1.find("apple");
  15. if(it1 != map_it_end) // instead of: if(it1 != map1.end())
  16. std::cout << it1->second << ", ";
  17. }
  18.  
  19. int main ()
  20. {
  21. auto it1 = map1.end();
  22. map1["aaa"] = "bbb";
  23. auto it2 = map1.end();
  24.  
  25. std::cout << std::endl << std::boolalpha << (it1 == it2) << std::endl;
  26.  
  27.  
  28. std::thread t1(func);
  29. std::thread t2(func);
  30. t1.join();
  31. t2.join();
  32.  
  33. return 0;
  34. }
Success #stdin #stdout 0s 20872KB
stdin
Standard input is empty
stdout
true
red, red,