From 61946b891f122abef6267f8c82e369c22154ecee Mon Sep 17 00:00:00 2001 From: Username404-59 Date: Tue, 6 Jul 2021 12:07:35 +0200 Subject: [PATCH] Use futures instead of threads to get better console output --- src/main.cpp | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 5bc653d..e6f84c7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,8 @@ #include #include #include +#include +#include using namespace std; #include "headers/misc.hpp" @@ -26,24 +28,26 @@ int main(int argc, char* argv[]) { } if (!files.empty()) { const set uniqueFiles(files.begin(), files.end()); - vector threads; + vector> units; for (string_view fileName: uniqueFiles) { if (fileName != "none") { - thread newThread = thread([printResult, fileName, target]() mutable { + future newAsync = async(launch::async, [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"; + stringstream consoleOutput; + if (printResult) consoleOutput << "~~~~[Yerbacon compilation result]~~~~\n\n" << "[WIP]\n" << transpiledString << "\n\n"; fileName.remove_suffix(6); string outputFile; (outputFile = fileName).append(target); outputFileContent(outputFile, transpiledString); + return consoleOutput.str(); }); - if (!parallel) newThread.join(); else { - threads.push_back(move(newThread)); + if (!parallel) cout << newAsync.get(); else { + units.push_back(move(newAsync)); } } } - for (auto& currentThread: threads) { - currentThread.join(); + for (auto& currentThread: units) { + cout << currentThread.get(); } } else cout << "No valid file provided.\n"; }