diff --git a/paddle/framework/scope.cc b/paddle/framework/scope.cc index 0c01d605bcd95f5796fba1e5a3351a2640b2898a..4e80e3d974e2b646ad62d26991e4629f8c450578 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 bcd8190755083ec30687675602a1c95a9c15c69e..c644e7d2966555ce45f81078c91ce419f3d20d0e 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