Created
August 19, 2017 12:26
-
-
Save kuenishi/5695f9ec838adb03f7d645a91a53eaef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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