From cf6fdc2f1fc8b5a7cbe717785d0b53305685e6fe Mon Sep 17 00:00:00 2001 From: Allan CORNET Date: Thu, 10 Oct 2024 18:38:28 +0200 Subject: [PATCH] remove use BS threadpool --- .../os_functions/src/cpp/SystemCommand.cpp | 44 ++++++++++++++----- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/modules/os_functions/src/cpp/SystemCommand.cpp b/modules/os_functions/src/cpp/SystemCommand.cpp index b7477e3a95..426e511528 100644 --- a/modules/os_functions/src/cpp/SystemCommand.cpp +++ b/modules/os_functions/src/cpp/SystemCommand.cpp @@ -17,7 +17,8 @@ #include #include #endif -#include + +// #include #include #include #include @@ -71,35 +72,57 @@ ParallelSystemCommand(const wstringVector& commands, const std::vector& size_t nbThreadsMax = (size_t)NelsonConfiguration::getInstance()->getMaxNumCompThreads(); size_t nbThreads = std::min(nbCommands, nbThreadsMax); + std::vector threads; std::vector taskList; - BS::thread_pool pool((BS::concurrency_t)nbThreads); + for (size_t k = 0; k < nbCommands; k++) { try { SystemCommandTask* task = new SystemCommandTask(); taskList.push_back(task); - pool.push_task(&SystemCommandTask::evaluateCommand, task, commands[k], timeouts[k]); + threads.emplace_back([task, commands, timeouts, k]() { + task->evaluateCommand(commands[k], timeouts[k]); + }); } catch (std::bad_alloc&) { Error(ERROR_MEMORY_ALLOCATION); } } - do { + + while (true) { if (NelsonConfiguration::getInstance()->getInterruptPending(evaluatorID)) { - for (size_t k = 0; k < nbCommands; k++) { - taskList[k]->terminate(); + for (SystemCommandTask* task : taskList) { + task->terminate(); } break; } + if (withEventsLoop) { ProcessEventsDynamicFunction(); } - std::this_thread::sleep_for(std::chrono::milliseconds(uint64(1))); - } while (pool.get_tasks_total()); + + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + + bool allTasksFinished = true; + for (SystemCommandTask* task : taskList) { + if (task->isRunning()) { + allTasksFinished = false; + break; + } + } + + if (allTasksFinished) { + for (auto& thread : threads) { + thread.join(); + } + break; + } + } for (size_t k = 0; k < nbCommands; k++) { if (taskList[k]) { results[k] = taskList[k]->getResult(); } } + for (SystemCommandTask* task : taskList) { if (task) { delete task; @@ -107,10 +130,9 @@ ParallelSystemCommand(const wstringVector& commands, const std::vector& } } taskList.clear(); - pool.reset((BS::concurrency_t)nbThreads); + return results; -} -//============================================================================= +} //============================================================================= static void initGuiDynamicLibrary() {