diff options
author | saturneric <[email protected]> | 2023-10-19 10:51:20 +0000 |
---|---|---|
committer | saturneric <[email protected]> | 2023-10-19 10:51:20 +0000 |
commit | 025c268f91ee1deab17891f00dc8c90c4770224f (patch) | |
tree | 3454263c7e5d4545bc5bac624eba17ca71257654 /src/core/thread/TaskRunner.cpp | |
parent | feat: using pool for concurrent executions, not stable yet (diff) | |
download | GpgFrontend-025c268f91ee1deab17891f00dc8c90c4770224f.tar.gz GpgFrontend-025c268f91ee1deab17891f00dc8c90c4770224f.zip |
fix: improve the stability of thread system
Diffstat (limited to 'src/core/thread/TaskRunner.cpp')
-rw-r--r-- | src/core/thread/TaskRunner.cpp | 42 |
1 files changed, 26 insertions, 16 deletions
diff --git a/src/core/thread/TaskRunner.cpp b/src/core/thread/TaskRunner.cpp index 494e356c..e8b85e93 100644 --- a/src/core/thread/TaskRunner.cpp +++ b/src/core/thread/TaskRunner.cpp @@ -48,30 +48,36 @@ class TaskRunner::Impl : public QThread { } task->setParent(nullptr); + task->moveToThread(this); - if (task->GetSequency()) { - SPDLOG_TRACE("post task: {}, sequency mode: {}", task->GetFullID(), - task->GetSequency()); - task->moveToThread(this); - } else { - 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()); - } + SPDLOG_TRACE("runner's pool starts task: {}", task->GetFullID()); + task->SafelyRun(); + } + + void PostConcurrentTask(Task* task) { + if (task == nullptr) { + SPDLOG_ERROR("task posted is null"); + return; } - emit task->SignalRun(); + + auto* concurrent_thread = new QThread(this); + + task->setParent(nullptr); + task->moveToThread(concurrent_thread); + + connect(task, &Task::SignalTaskEnd, concurrent_thread, &QThread::quit); + connect(concurrent_thread, &QThread::finished, concurrent_thread, + &QThread::deleteLater); + + concurrent_thread->start(); + + task->SafelyRun(); } void PostScheduleTask(Task* task, size_t seconds) { if (task == nullptr) return; // TODO } - - private: - QThreadPool pool_; }; GpgFrontend::Thread::TaskRunner::TaskRunner() : p_(std::make_unique<Impl>()) {} @@ -82,6 +88,10 @@ void GpgFrontend::Thread::TaskRunner::PostTask(Task* task) { p_->PostTask(task); } +void GpgFrontend::Thread::TaskRunner::PostConcurrentTask(Task* task) { + p_->PostConcurrentTask(task); +} + void TaskRunner::PostScheduleTask(Task* task, size_t seconds) { p_->PostScheduleTask(task, seconds); } |