From 314cff7f494bbbccb93ff8f84368cf815be1b847 Mon Sep 17 00:00:00 2001 From: Username404-59 Date: Tue, 6 Jul 2021 11:37:43 +0200 Subject: [PATCH] Add a --parallel argument to enable multithreading. --- src/main.cpp | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index c752f0c..5bc653d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,6 @@ #include #include +#include using namespace std; #include "headers/misc.hpp" @@ -9,6 +10,7 @@ int main(int argc, char* argv[]) { string target = ".lua"; string_view currentArg; bool printResult = false; + bool parallel = false; if (argc > 0) { vector files; for (signed int i = 0; i < argc; ++i) @@ -19,20 +21,30 @@ int main(int argc, char* argv[]) { currentArg.remove_prefix(9); (target = '.') += currentArg; } + else if (currentArg == "--parallel") parallel = true; else if (currentArg.ends_with(".ybcon")) files.push_back(currentArg); } if (!files.empty()) { const set uniqueFiles(files.begin(), files.end()); + vector threads; for (string_view fileName: uniqueFiles) { if (fileName != "none") { - const string transpiledString = transpile(parseString(getFileContent(fileName.data())), target); - if (printResult) cout << "~~~~[Yerbacon compilation result]~~~~\n\n" << "[WIP]\n" << transpiledString << "\n\n"; - fileName.remove_suffix(6); - string outputFile; - (outputFile = fileName).append(target); - outputFileContent(outputFile, transpiledString); + thread newThread = thread([printResult, fileName, target]() mutable { + const string transpiledString = transpile(parseString(getFileContent(fileName.data())), target); + if (printResult) cout << "~~~~[Yerbacon compilation result]~~~~\n\n" << "[WIP]\n" << transpiledString << "\n\n"; + fileName.remove_suffix(6); + string outputFile; + (outputFile = fileName).append(target); + outputFileContent(outputFile, transpiledString); + }); + if (!parallel) newThread.join(); else { + threads.push_back(move(newThread)); + } } } + for (auto& currentThread: threads) { + currentThread.join(); + } } else cout << "No valid file provided.\n"; } return EXIT_SUCCESS;