diff options
author | Daniel Wilhelm <shieldwed@outlook.com> | 2017-01-08 18:21:23 +0100 |
---|---|---|
committer | Daniel Wilhelm <shieldwed@outlook.com> | 2017-01-08 18:21:23 +0100 |
commit | fe660cdff59aa3a939479ed60172e5c0803552b2 (patch) | |
tree | 045cf295b79de10f75ed6362c5836db25c9fc63a /wx+/async_task.h | |
parent | 8.6 (diff) | |
download | FreeFileSync-fe660cdff59aa3a939479ed60172e5c0803552b2.tar.gz FreeFileSync-fe660cdff59aa3a939479ed60172e5c0803552b2.tar.bz2 FreeFileSync-fe660cdff59aa3a939479ed60172e5c0803552b2.zip |
8.7
Diffstat (limited to 'wx+/async_task.h')
-rw-r--r-- | wx+/async_task.h | 39 |
1 files changed, 21 insertions, 18 deletions
diff --git a/wx+/async_task.h b/wx+/async_task.h index 915f9602..7ac03949 100644 --- a/wx+/async_task.h +++ b/wx+/async_task.h @@ -24,6 +24,9 @@ Run a task in an async thread, but process result in GUI event loop 2. schedule async task and synchronous continuation: guiQueue.processAsync(evalAsync, evalOnGui); + +Alternative: use wxWidgets' inter-thread communication (wxEvtHandler::QueueEvent) https://wiki.wxwidgets.org/Inter-Thread_and_Inter-Process_communication + => don't bother, probably too many MT race conditions lurking around */ namespace impl @@ -74,7 +77,7 @@ public: void add(Fun&& evalAsync, Fun2&& evalOnGui) { using ResultType = decltype(evalAsync()); - tasks.push_back(std::make_unique<ConcreteTask<ResultType, Fun2>>(zen::runAsync(std::forward<Fun>(evalAsync)), std::forward<Fun2>(evalOnGui))); + tasks_.push_back(std::make_unique<ConcreteTask<ResultType, Fun2>>(zen::runAsync(std::forward<Fun>(evalAsync)), std::forward<Fun2>(evalOnGui))); } //equivalent to "evalOnGui(evalAsync())" // -> evalAsync: the usual thread-safety requirements apply! @@ -82,14 +85,14 @@ public: void evalResults() //call from gui thread repreatedly { - if (!inRecursion) //prevent implicit recursion, e.g. if we're called from an idle event and spawn another one within the callback below + if (!inRecursion_) //prevent implicit recursion, e.g. if we're called from an idle event and spawn another one within the callback below { - inRecursion = true; - ZEN_ON_SCOPE_EXIT(inRecursion = false); + inRecursion_ = true; + ZEN_ON_SCOPE_EXIT(inRecursion_ = false); std::vector<std::unique_ptr<Task>> readyTasks; //Reentrancy; access to AsyncTasks::add is not protected! => evaluate outside erase_if - erase_if(tasks, [&](std::unique_ptr<Task>& task) + erase_if(tasks_, [&](std::unique_ptr<Task>& task) { if (task->resultReady()) { @@ -104,14 +107,14 @@ public: } } - bool empty() const { return tasks.empty(); } + bool empty() const { return tasks_.empty(); } private: AsyncTasks (const AsyncTasks&) = delete; AsyncTasks& operator=(const AsyncTasks&) = delete; - bool inRecursion = false; - std::vector<std::unique_ptr<Task>> tasks; + bool inRecursion_ = false; + std::vector<std::unique_ptr<Task>> tasks_; }; } @@ -119,27 +122,27 @@ private: class AsyncGuiQueue : private wxEvtHandler { public: - AsyncGuiQueue() { timer.Connect(wxEVT_TIMER, wxEventHandler(AsyncGuiQueue::onTimerEvent), nullptr, this); } + AsyncGuiQueue() { timer_.Connect(wxEVT_TIMER, wxEventHandler(AsyncGuiQueue::onTimerEvent), nullptr, this); } template <class Fun, class Fun2> void processAsync(Fun&& evalAsync, Fun2&& evalOnGui) { - asyncTasks.add(std::forward<Fun >(evalAsync), - std::forward<Fun2>(evalOnGui)); - if (!timer.IsRunning()) - timer.Start(50 /*unit: [ms]*/); + asyncTasks_.add(std::forward<Fun >(evalAsync), + std::forward<Fun2>(evalOnGui)); + if (!timer_.IsRunning()) + timer_.Start(50 /*unit: [ms]*/); } private: void onTimerEvent(wxEvent& event) //schedule and run long-running tasks asynchronously { - asyncTasks.evalResults(); //process results on GUI queue - if (asyncTasks.empty()) - timer.Stop(); + asyncTasks_.evalResults(); //process results on GUI queue + if (asyncTasks_.empty()) + timer_.Stop(); } - impl::AsyncTasks asyncTasks; - wxTimer timer; //don't use wxWidgets' idle handling => repeated idle requests/consumption hogs 100% cpu! + impl::AsyncTasks asyncTasks_; + wxTimer timer_; //don't use wxWidgets' idle handling => repeated idle requests/consumption hogs 100% cpu! }; } |