diff --git a/Makefile.am b/Makefile.am
index 0dc576c..e24ec7b 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -17,7 +17,7 @@
# .
SUBDIRS = tests
-AM_CXXFLAGS = -std=c++11 -Wall -Wextra -pedantic -pthread
+AM_CXXFLAGS = -std=c++20 -Wall -Wextra -pedantic -pthread
bin_PROGRAMS = termux-elf-cleaner
diff --git a/elf-cleaner.cpp b/elf-cleaner.cpp
index e4c6031..67c9cfe 100644
--- a/elf-cleaner.cpp
+++ b/elf-cleaner.cpp
@@ -33,8 +33,8 @@ along with termux-elf-cleaner. If not, see
#include
#include
-#include
-#include
+#include
+#include
#include
#include
@@ -64,8 +64,6 @@ int api_level = 21;
bool dry_run = false;
bool quiet = false;
-std::mutex mutex;
-
static char const *const usage_message[] =
{ "\
\n\
@@ -311,16 +309,6 @@ int parse_file(const char *file_name)
return 0;
}
-void parse_file_handler(std::deque* files, unsigned int &pos) {
- while (files->size() > pos) {
- mutex.lock();
- const char *file = files->at(pos);
- pos++;
- mutex.unlock();
- parse_file(file);
- }
-}
-
int main(int argc, char **argv)
{
int skip_args = 0;
@@ -362,15 +350,19 @@ int main(int argc, char **argv)
if (argc - (skip_args + 1) <= threads_count) threads_count = files_count;
if (threads_count < 1) threads_count = 1;
- std::deque files;
- std::vector threads(threads_count);
- unsigned int pos = 0;
+ std::vector> futures;
+ std::counting_semaphore sem(threads_count);
+
+ for (int i = skip_args + 1; i < argc; i++) {
+ sem.acquire();
+ const char* file = argv[i];
+ futures.push_back(std::async([file, &sem]() {
+ parse_file(file);
+ sem.release();
+ }));
+ }
+
+ for (auto& future : futures) future.get();
- for (int i = skip_args + 1; i < argc; i++)
- files.push_back(argv[i]);
- for (int i = 0; i < threads_count; i++)
- threads[i] = std::thread(parse_file_handler, &files, std::ref(pos));
- for (std::thread& thread : threads)
- if (thread.joinable()) thread.join();
return 0;
}