#include <iostream>
#include <string>
#include <mutex>
#include <thread>
#include <map>
std::map<std::string, std::string> map1 ( {{"apple","red"},{"lemon","yellow"}} );
static auto const map_it_end = map1.end();
std::mutex mtx1;
void func() {
std::lock_guard<std::mutex> lock1(mtx1);
auto it1 = map1.find("apple");
if(it1 != map_it_end) // instead of: if(it1 != map1.end())
std::cout << it1->second << ", ";
}
int main ()
{
auto it1 = map1.end();
map1["aaa"] = "bbb";
auto it2 = map1.end();
std::cout << std::endl << std::boolalpha << (it1 == it2) << std::endl;
std::thread t1(func);
std::thread t2(func);
t1.join();
t2.join();
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgojaW5jbHVkZSA8bXV0ZXg+CiNpbmNsdWRlIDx0aHJlYWQ+CiNpbmNsdWRlIDxtYXA+CgpzdGQ6Om1hcDxzdGQ6OnN0cmluZywgc3RkOjpzdHJpbmc+IG1hcDEgKCB7eyJhcHBsZSIsInJlZCJ9LHsibGVtb24iLCJ5ZWxsb3cifX0gKTsKc3RhdGljIGF1dG8gY29uc3QgbWFwX2l0X2VuZCA9IG1hcDEuZW5kKCk7CnN0ZDo6bXV0ZXggbXR4MTsKCnZvaWQgZnVuYygpIHsKICAgIHN0ZDo6bG9ja19ndWFyZDxzdGQ6Om11dGV4PiBsb2NrMShtdHgxKTsKCiAgICBhdXRvIGl0MSA9IG1hcDEuZmluZCgiYXBwbGUiKTsKICAgIGlmKGl0MSAhPSBtYXBfaXRfZW5kKQkvLyBpbnN0ZWFkIG9mOiBpZihpdDEgIT0gbWFwMS5lbmQoKSkKICAgICAgICBzdGQ6OmNvdXQgPDwgaXQxLT5zZWNvbmQgPDwgIiwgIjsKfQoKaW50IG1haW4gKCkKewoJYXV0byBpdDEgPSBtYXAxLmVuZCgpOwoJbWFwMVsiYWFhIl0gPSAiYmJiIjsKCWF1dG8gaXQyID0gbWFwMS5lbmQoKTsKCQoJc3RkOjpjb3V0IDw8IHN0ZDo6ZW5kbCA8PCBzdGQ6OmJvb2xhbHBoYSA8PCAoaXQxID09IGl0MikgPDwgc3RkOjplbmRsOwoJCgkKICAgIHN0ZDo6dGhyZWFkIHQxKGZ1bmMpOwogICAgc3RkOjp0aHJlYWQgdDIoZnVuYyk7CiAgICB0MS5qb2luKCk7CiAgICB0Mi5qb2luKCk7CgogICAgcmV0dXJuIDA7Cn0=