Advertisement
Guest User

Untitled

a guest
Apr 8th, 2019
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.92 KB | None | 0 0
  1. #include <thread>
  2. #include <fstream>
  3. #include <unordered_map>
  4. #include <iostream>
  5. #include <vector>
  6. #include <mutex>
  7.    
  8. template<class Callback>
  9. struct WordCounter {
  10.     Callback callback;
  11.  
  12.     void operator()(std::string filename) {
  13.         std::ifstream myFile(filename);
  14.         if (myFile) {
  15.             countWords(myFile);
  16.         }
  17.         else {
  18.             std::cerr << "Unable to open " + filename << '\n';
  19.         }
  20.     }
  21.     void countWords(std::ifstream& filestream) {
  22.         std::unordered_map<std::string, int> wordCount;
  23.         std::string word;
  24.         while (!filestream.eof() && !filestream.fail()) {
  25.             filestream >> word;
  26.             wordCount[word] += 1;
  27.         }
  28.         callback(wordCount);
  29.     }
  30. };
  31.  
  32. template<class Callback>
  33. WordCounter<Callback> makeWordCounter(Callback const& func) {
  34.     return WordCounter<Callback>{func};
  35. }
  36.  
  37. using std::unordered_map;
  38. using std::string;
  39. using std::vector;
  40.  
  41. unordered_map<string, int> countWords(vector<string> const& filenames) {
  42.     vector<std::thread> threads;
  43.     threads.reserve(filenames.size());
  44.  
  45.     std::mutex map_lock;
  46.     std::unordered_map<std::string, int> totalWordCount;
  47.  
  48.     // Define the callback function
  49.     // This operation is basically free
  50.     // Internally, it just copies a reference to the mutex and a reference
  51.     // to the totalWordCount
  52.     auto callback = [&](unordered_map<string, int> const& partial_count) {
  53.         map_lock.lock();
  54.         for(auto count : partial_count) {
  55.             totalWordCount[count.first] += count.second;
  56.         }
  57.         map_lock.unlock();
  58.     };
  59.  
  60.     // Create a new thread for each file
  61.     for(auto& file : filenames) {
  62.         threads.push_back(std::thread(makeWordCounter(callback), file));
  63.     }
  64.  
  65.     // Wait until all threads have finished
  66.     for(auto& thread : threads) {
  67.         thread.join();
  68.     }
  69.  
  70.     return totalWordCount;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement