#include <iostream>
#include <thread>
#include <mutex>
#include <map>
#include <string>
std::map<std::string, std::string> g_pages;
std::mutex g_pages_mutex;
void save_page(const std::string &url)
{
std::string result = "fake content";
g_pages_mutex.lock();
try{
g_pages[url] = result;
if( url.find("thrd2", 0) != std::string::npos )
throw "I'm naipnulosya";
}
catch(const char * msg){
g_pages[url] = msg;
}
g_pages_mutex.unlock();
}
int main()
{
std::thread t1(save_page, "http://thrd1URL");
std::thread t2(save_page, "http://thrd2URL");
t1.join();
t2.join();
g_pages_mutex.lock();
for (const auto &pair : g_pages) {
std::cout << pair.first << " => " << pair.second << '\n';
}
g_pages_mutex.unlock();
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dGhyZWFkPgojaW5jbHVkZSA8bXV0ZXg+CiNpbmNsdWRlIDxtYXA+CiNpbmNsdWRlIDxzdHJpbmc+CiAKc3RkOjptYXA8c3RkOjpzdHJpbmcsIHN0ZDo6c3RyaW5nPiBnX3BhZ2VzOwpzdGQ6Om11dGV4IGdfcGFnZXNfbXV0ZXg7CiAKdm9pZCBzYXZlX3BhZ2UoY29uc3Qgc3RkOjpzdHJpbmcgJnVybCkKewogICAgc3RkOjpzdHJpbmcgcmVzdWx0ID0gImZha2UgY29udGVudCI7CiAKICAgIGdfcGFnZXNfbXV0ZXgubG9jaygpOwogICAgdHJ5ewogICAgCWdfcGFnZXNbdXJsXSA9IHJlc3VsdDsKICAgIAlpZiggdXJsLmZpbmQoInRocmQyIiwgMCkgIT0gc3RkOjpzdHJpbmc6Om5wb3MgKQogICAgCQl0aHJvdyAiSSdtIG5haXBudWxvc3lhIjsKICAgIH0KICAgIGNhdGNoKGNvbnN0IGNoYXIgKiBtc2cpewogICAgCSBnX3BhZ2VzW3VybF0gPSBtc2c7CiAgICB9CiAgICBnX3BhZ2VzX211dGV4LnVubG9jaygpOwp9CiAKaW50IG1haW4oKQp7CiAgICBzdGQ6OnRocmVhZCB0MShzYXZlX3BhZ2UsICJodHRwO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