Skip to content

Commit

Permalink
remove use BS threadpool
Browse files Browse the repository at this point in the history
  • Loading branch information
Nelson-numerical-software committed Oct 10, 2024
1 parent b3b3fcd commit cf6fdc2
Showing 1 changed file with 33 additions and 11 deletions.
44 changes: 33 additions & 11 deletions modules/os_functions/src/cpp/SystemCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
#include <fcntl.h>
#include <csignal>
#endif
#include <BS_thread_pool.hpp>

// #include <BS_thread_pool.hpp>
#include <ctime>
#include <thread>
#include <chrono>
Expand Down Expand Up @@ -71,46 +72,67 @@ ParallelSystemCommand(const wstringVector& commands, const std::vector<uint64>&
size_t nbThreadsMax = (size_t)NelsonConfiguration::getInstance()->getMaxNumCompThreads();
size_t nbThreads = std::min(nbCommands, nbThreadsMax);

std::vector<std::thread> threads;
std::vector<SystemCommandTask*> 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;
task = nullptr;
}
}
taskList.clear();
pool.reset((BS::concurrency_t)nbThreads);

return results;
}
//=============================================================================
} //=============================================================================
static void
initGuiDynamicLibrary()
{
Expand Down

0 comments on commit cf6fdc2

Please sign in to comment.