From 8ae84a5745591a2937d3004831050f8fe77ce1ef Mon Sep 17 00:00:00 2001 From: Yang Yu Date: Thu, 4 Jan 2018 14:47:35 +0800 Subject: [PATCH] Async to drop kid --- paddle/framework/scope.cc | 4 +++- paddle/framework/threadpool.h | 9 +++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/paddle/framework/scope.cc b/paddle/framework/scope.cc index 0c01d605bcd..4e80e3d974e 100644 --- a/paddle/framework/scope.cc +++ b/paddle/framework/scope.cc @@ -17,6 +17,7 @@ limitations under the License. */ #include // for unique_ptr #include // for call_once #include "glog/logging.h" +#include "paddle/framework/threadpool.h" #include "paddle/string/printf.h" namespace paddle { @@ -87,7 +88,8 @@ void Scope::DeleteScope(Scope* scope) { auto it = std::find(this->kids_.begin(), this->kids_.end(), scope); PADDLE_ENFORCE(it != this->kids_.end(), "Cannot find %p as kid scope", scope); this->kids_.erase(it); - delete scope; + // Make delete async. + Async([scope] { delete scope; }); } void Scope::Rename(const std::string& origin_name, diff --git a/paddle/framework/threadpool.h b/paddle/framework/threadpool.h index bcd81907550..c644e7d2966 100644 --- a/paddle/framework/threadpool.h +++ b/paddle/framework/threadpool.h @@ -159,5 +159,14 @@ class ThreadPool { std::condition_variable completed_; }; +// Run a function asynchronously. +// NOTE: The function must return void. If the function need to return a value, +// you can use lambda to capture a value pointer. +template +std::future Async(Callback callback, ARGS... args) { + return ThreadPool::GetInstance()->Run( + [&] { callback(std::forward(args)...); }); +}; + } // namespace framework } // namespace paddle -- GitLab