-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
43 lines (38 loc) · 1.04 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//Massively Parallel Directory Listing
//Not for Production (Learning)
#include <filesystem>
#include <iostream>
#include <thread>
#include <future>
#include <string>
#include <vector>
std::vector<std::string> files;
void listDir(const std::string &root){
std::filesystem::directory_iterator dir_itr(root);
std::vector<std::future<void> > futures;
for(auto &dir_entry: dir_itr){
//don't process hidden dirs
std::string fname = dir_entry.path().filename();
if(fname[0] == '.'){
continue;
}
if(dir_entry.is_directory()){
std::future<void> ftr = std::async(std::launch::async, &listDir, dir_entry.path().string());
futures.push_back(std::move(ftr));
}
else{
files.push_back(dir_entry.path().string());
}
}
for(auto &&future: futures){
future.wait();
}
}
int main(){
std::string rootdir = "/home/shikhar/";
listDir(rootdir);
for(const auto s: files){
std::cout << s << std::endl;
}
return 0;
}