Use futures instead of threads to get better console output
This commit is contained in:
parent
314cff7f49
commit
61946b891f
18
src/main.cpp
18
src/main.cpp
|
@ -1,6 +1,8 @@
|
|||
#include <iostream>
|
||||
#include <set>
|
||||
#include <thread>
|
||||
#include <future>
|
||||
#include <sstream>
|
||||
using namespace std;
|
||||
|
||||
#include "headers/misc.hpp"
|
||||
|
@ -26,24 +28,26 @@ int main(int argc, char* argv[]) {
|
|||
}
|
||||
if (!files.empty()) {
|
||||
const set<string_view> uniqueFiles(files.begin(), files.end());
|
||||
vector<thread> threads;
|
||||
vector<future<string>> units;
|
||||
for (string_view fileName: uniqueFiles) {
|
||||
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);
|
||||
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";
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue