daily pastebin goal
60%
SHARE
TWEET

Untitled

a guest Dec 25th, 2018 8 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <chrono>
  3. #include <thread>
  4. #include <mutex>
  5. #include <map>
  6. #include <string>
  7.  
  8. std::map<std::string, std::string> g_pages;
  9. std::mutex g_pages_mutex;
  10.  
  11. void save_page(const std::string &url)
  12. {
  13.     // simulate a long page fetch
  14.     std::this_thread::sleep_for(std::chrono::seconds(2));
  15.     std::string result = "fake content";
  16.  
  17.     g_pages_mutex.lock();
  18.     g_pages[url] = result;
  19.     g_pages_mutex.unlock();
  20. }
  21.  
  22. int main()
  23. {
  24.     std::thread t1(save_page, "http://foo");
  25.     std::thread t2(save_page, "http://bar");
  26.     t1.join();
  27.     t2.join();
  28.  
  29.     g_pages_mutex.lock();
  30.     for (const auto &pair : g_pages) {
  31.         std::cout << pair.first << " => " << pair.second << '\n';
  32.     }
  33.     g_pages_mutex.unlock();
  34. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top