Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <fstream>
- #include <iostream>
- #include <string>
- #include <vector>
- #include <map>
- #include<mutex>
- #include <future>
- #include <unordered_map>
- #include <windows.h>
- using namespace std;
- const string DIR = "C:/Users/aiden/Desktop/Word Frequency Analyzer/";
- int main(void)
- {
- vector<string> fileName = { "input2.txt","input1.txt"};
- static vector<pair<int, string>> finalWordCount;
- static vector<thread> workers;
- mutex lock_finalWordCount;
- for (string& item : fileName)
- {
- std::string currentFile = DIR + item;
- cout << currentFile << endl;
- workers.push_back(thread([¤tFile, &lock_finalWordCount]{
- cout << "Running thread" <<"n";
- unordered_map<string, int>thisWordCount;
- ifstream filestream;
- string word;
- filestream.open(currentFile);
- if (!filestream) {
- cout << "Unable to open file" << endl;
- exit(1);
- }
- else if (filestream.is_open()) {
- while (filestream >> word)
- {
- ++thisWordCount[word];
- }
- }
- filestream.close();
- cout << "n" << "Locking reasource " << "n";
- lock_finalWordCount.lock();
- for (auto item : thisWordCount) {
- if (item.second > 1) { finalWordCount.push_back(pair<int,string>(item.second, item.first)); }
- }
- lock_finalWordCount.unlock();
- cout << "n" << "releasing reasource " << "n";//for debugging
- }));
- }
- for (auto& thread : workers) {
- thread.join();
- }
- sort(finalWordCount.begin(), finalWordCount.end(), [](const pair<int, string> &a, const pair<int, string> &b){
- return (a.first > b.first);
- });
- cout << flush;
- system("CLS");
- cout << "____________________" << endl;
- for (const auto &it : finalWordCount) {
- cout << it.first << "t" << it.second << "n";
- }
- cout << "____________________" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement