You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@celix.apache.org by pn...@apache.org on 2022/06/26 13:10:55 UTC
[celix] 01/01: Refactors the DefaultExecutor of celix::Promises so that std::function goes out of scope after executing
This is an automated email from the ASF dual-hosted git repository.
pnoltes pushed a commit to branch feature/update_default_promise_executor
in repository https://gitbox.apache.org/repos/asf/celix.git
commit d33a6a0f334346466db64bf857dc2694ac8a9409
Author: Pepijn Noltes <pe...@gmail.com>
AuthorDate: Sun Jun 26 15:10:49 2022 +0200
Refactors the DefaultExecutor of celix::Promises so that std::function goes out of scope after executing
---
libs/promises/api/celix/DefaultExecutor.h | 5 ++++-
libs/promises/api/celix/IExecutor.h | 3 +++
2 files changed, 7 insertions(+), 1 deletion(-)
diff --git a/libs/promises/api/celix/DefaultExecutor.h b/libs/promises/api/celix/DefaultExecutor.h
index d49307cc..294f89fb 100644
--- a/libs/promises/api/celix/DefaultExecutor.h
+++ b/libs/promises/api/celix/DefaultExecutor.h
@@ -36,7 +36,10 @@ namespace celix {
void execute(int /*priority*/, std::function<void()> task) override {
std::lock_guard lck{mutex};
- futures.emplace_back(std::async(policy, std::move(task)));
+ futures.emplace_back(std::async(policy, [task = std::move(task)]() mutable {
+ task();
+ task = nullptr; //to ensure captures of task go out of scope
+ }));
removeCompletedFutures();
}
diff --git a/libs/promises/api/celix/IExecutor.h b/libs/promises/api/celix/IExecutor.h
index 7c9e5c0b..5cae91f7 100644
--- a/libs/promises/api/celix/IExecutor.h
+++ b/libs/promises/api/celix/IExecutor.h
@@ -39,6 +39,9 @@ namespace celix {
* @brief Executes the given command at some time in the future. The command may execute in a new thread,
* in a pooled thread, or in the calling thread, at the discretion of the Executor implementation.
*
+ * @note After a task has been executed, the `std::function<void()>` task object must go out of scope to
+ * ensure that the potential capture objects also go out of scope.
+ *
* @param priority the priority of the task. It depends on the executor implementation whether this is supported.
* @param command the "runnable" task
* @throws celix::RejectedExecutionException if this task cannot be accepted for execution.