-
Notifications
You must be signed in to change notification settings - Fork 2
/
FilePolling.cpp
68 lines (62 loc) · 1.57 KB
/
FilePolling.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <string>
#include <future>
#include <mutex>
#include <vector>
#include "boost/filesystem.hpp"
using namespace boost::filesystem;
class FileMonitor {
public:
void push_back(std::string file) {
std::lock_guard<std::mutex> lck(_mtx);
_allFiles.push_back(std::move(file));
}
bool hasData() const {
std::lock_guard<std::mutex> lck(_mtx);
return !_allFiles.empty();
}
std::string pop_back() {
std::lock_guard<std::mutex> lck(_mtx);
std::string name = move(_allFiles.back());
_allFiles.pop_back();
return name;
}
private:
mutable std::mutex _mtx;
std::vector<std::string> _allFiles;
};
void listDir(path pth, FileMonitor& fileSink) {
for (directory_iterator it(pth); it != directory_iterator(); ++it) {
if (is_regular_file(it->status())) {
fileSink.push_back(it->path().filename().string());
}
}
}
void listDirs(std::vector<path> paths, FileMonitor& fileSink) {
std::vector<std::future<void>> futures;
for (auto& pth : paths) {
std::cout << pth << std::endl;
futures.emplace_back(std::async(listDir, pth, std::ref(fileSink)));
}
for (;;) {
if (fileSink.hasData()) {
std::string name = fileSink.pop_back();
std::cout << name << std::endl;
}
}
for (auto& fut : futures) {
fut.wait();
}
}
int main() {
std::vector<path> paths;
for (directory_iterator it(std::getenv("HOME")); it != directory_iterator();
++it) {
if (is_directory(it->status())) {
paths.push_back(it->path());
}
}
FileMonitor fileSink;
listDirs(paths, fileSink);
return 0;
}