Use futures instead of threads to get better console output

This commit is contained in:
Username404-59 2021-07-06 12:07:35 +02:00
parent 314cff7f49
commit 61946b891f
Signed by: Username404-59
GPG Key ID: 7AB361FBB257A5D1
1 changed files with 11 additions and 7 deletions

View File

@ -1,6 +1,8 @@
#include <iostream> #include <iostream>
#include <set> #include <set>
#include <thread> #include <thread>
#include <future>
#include <sstream>
using namespace std; using namespace std;
#include "headers/misc.hpp" #include "headers/misc.hpp"
@ -26,24 +28,26 @@ int main(int argc, char* argv[]) {
} }
if (!files.empty()) { if (!files.empty()) {
const set<string_view> uniqueFiles(files.begin(), files.end()); const set<string_view> uniqueFiles(files.begin(), files.end());
vector<thread> threads; vector<future<string>> units;
for (string_view fileName: uniqueFiles) { for (string_view fileName: uniqueFiles) {
if (fileName != "none") { if (fileName != "none") {
thread newThread = thread([printResult, fileName, target]() mutable { future<string> newAsync = async(launch::async, [printResult, fileName, target]() mutable {
const string transpiledString = transpile(parseString(getFileContent(fileName.data())), target); 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); fileName.remove_suffix(6);
string outputFile; string outputFile;
(outputFile = fileName).append(target); (outputFile = fileName).append(target);
outputFileContent(outputFile, transpiledString); outputFileContent(outputFile, transpiledString);
return consoleOutput.str();
}); });
if (!parallel) newThread.join(); else { if (!parallel) cout << newAsync.get(); else {
threads.push_back(move(newThread)); units.push_back(move(newAsync));
} }
} }
} }
for (auto& currentThread: threads) { for (auto& currentThread: units) {
currentThread.join(); cout << currentThread.get();
} }
} else cout << "No valid file provided.\n"; } else cout << "No valid file provided.\n";
} }