Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
#include <iostream>
#include <thread>
#include <vector>
#include <mutex>
// Requires C++17, not included in libstdc++.a
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
// $ c++ -std=c++17 prm.cc -lpthread -lstdc++fs -g
std::mutex dirs_mutex;
std::vector<std::string> dirs;
void rm_rf_worker() {
std::string name;
while(true){
{
std::lock_guard<std::mutex> lock(dirs_mutex);
if (dirs.empty()) {
return;
}
name = dirs.front();
dirs.erase(dirs.begin());
}
std::cout << "rm-rf ing " << name << std::endl;
fs::remove_all(name);
}
}
int main(int args, char** argv) {
if (args < 2) {
printf("Needs argv\n");
return -1;
}
printf("Directory to delete: %s\n", argv[1]);
for (auto & p : std::experimental::filesystem::directory_iterator(argv[1])) {
std::lock_guard<std::mutex> lock(dirs_mutex);
dirs.push_back(p.path().string());
}
const unsigned int N = 8;
std::thread threads[N];
for (size_t s = 0; s < N; ++s) {
threads[s] = std::thread(rm_rf_worker);
}
for (size_t s = 0; s < N; ++s) {
threads[s].join();
}
// fs::remove_all(argv[1]);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment