diff options
author | saturneric <[email protected]> | 2023-10-18 14:45:33 +0000 |
---|---|---|
committer | saturneric <[email protected]> | 2023-10-18 14:45:33 +0000 |
commit | 70196cf01757824a578e4d9c49a210bf136de266 (patch) | |
tree | e058a59c6289b2a3872222c8822bae393cb7c213 /src/core/thread/TaskRunner.cpp | |
parent | fix: solve build issues on macOS (diff) | |
download | GpgFrontend-70196cf01757824a578e4d9c49a210bf136de266.tar.gz GpgFrontend-70196cf01757824a578e4d9c49a210bf136de266.zip |
feat: using pool for concurrent executions, not stable yet
Diffstat (limited to 'src/core/thread/TaskRunner.cpp')
-rw-r--r-- | src/core/thread/TaskRunner.cpp | 26 |
1 files changed, 11 insertions, 15 deletions
diff --git a/src/core/thread/TaskRunner.cpp b/src/core/thread/TaskRunner.cpp index 6eed7a89..494e356c 100644 --- a/src/core/thread/TaskRunner.cpp +++ b/src/core/thread/TaskRunner.cpp @@ -30,6 +30,7 @@ #include <qobjectdefs.h> #include <qthread.h> +#include <qthreadpool.h> #include <memory> @@ -47,27 +48,19 @@ class TaskRunner::Impl : public QThread { } task->setParent(nullptr); - connect(task, &Task::SignalTaskEnd, task, &Task::deleteLater); if (task->GetSequency()) { SPDLOG_TRACE("post task: {}, sequency mode: {}", task->GetFullID(), task->GetSequency()); task->moveToThread(this); } else { - // if it need to run concurrently, we should create a new thread to - // run it. - auto* concurrent_thread = new QThread(this); - - connect(task, &Task::SignalTaskEnd, concurrent_thread, &QThread::quit); - // concurrent thread is responsible for deleting the task - connect(concurrent_thread, &QThread::finished, task, &Task::deleteLater); - // concurrent thread is responsible for self deleting - connect(concurrent_thread, &QThread::finished, concurrent_thread, - &QThread::deleteLater); - - // start thread - concurrent_thread->start(); - task->moveToThread(concurrent_thread); + if (pool_.tryStart(task)) { + SPDLOG_TRACE("runner's pool starts concurrent task {} immediately", + task->GetFullID()); + } else { + SPDLOG_TRACE("runner's pool will start concurrent task {} later", + task->GetFullID()); + } } emit task->SignalRun(); } @@ -76,6 +69,9 @@ class TaskRunner::Impl : public QThread { if (task == nullptr) return; // TODO } + + private: + QThreadPool pool_; }; GpgFrontend::Thread::TaskRunner::TaskRunner() : p_(std::make_unique<Impl>()) {} |