Skip to content

Instantly share code, notes, and snippets.

@jnovikov
Created January 13, 2019 10:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jnovikov/0af6b9752ceb085357f18ae6224abd0d to your computer and use it in GitHub Desktop.
Save jnovikov/0af6b9752ceb085357f18ae6224abd0d to your computer and use it in GitHub Desktop.
Threads SHP example
#include <thread>
#include <iostream>
#include <fstream>
#include <cmath>
#include <vector>
#include <map>
//
//std::map<std::string, int> stats;
//
//void count_words(std::string& filename) {
// std::cout << "Started reading file " << filename << std::endl;
// std::ifstream f(filename);
// int counter = 0;
// std::string temp;
// while (f >> temp) {
// counter++;
// }
// f.close();
// stats[filename] = counter;
// std::cout << "Finished reading file " << filename << std::endl;
//}
//
//
//
//
//int main() {
// std::vector<std::string> files = {"/etc/passwd", "kek.txt"};
// std::vector<std::thread> threads;
// for (std::string& s: files) {
// threads.emplace_back(std::thread(count_words,std::ref(s)));
//
// }
// for (std::thread& t: threads) {
// t.join();
// }
// for (auto p: stats) {
// std::cout << p.first << " has " << p.second << " words" << std::endl;
// }
// return 0;
//}
std::atomic<int> counter;
void inc_counter() {
counter++;
}
int main() {
counter = 0;
std::vector<std::thread> threads;
std::cout << counter << std::endl;
for (int i = 0 ; i < 1000; i++) {
threads.emplace_back(std::thread(inc_counter));
}
for (std::thread& t: threads) {
t.join();
}
std::cout << counter << std::endl;
std::cout << threads.size() << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment