From 05d6783e08b0118f7d9545c8503170df099ead91 Mon Sep 17 00:00:00 2001 From: Ara Ayvazyan Date: Mon, 8 Jan 2018 17:41:53 -0800 Subject: [PATCH] [c++ grpc] Simplify io_manager::wait --- cpp/inc/bond/ext/grpc/io_manager.h | 32 +++++++++++------------------- 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/cpp/inc/bond/ext/grpc/io_manager.h b/cpp/inc/bond/ext/grpc/io_manager.h index c139cc39..e4dba9ec 100644 --- a/cpp/inc/bond/ext/grpc/io_manager.h +++ b/cpp/inc/bond/ext/grpc/io_manager.h @@ -5,7 +5,7 @@ #include -#include +#include #include #ifdef _MSC_VER @@ -179,25 +179,18 @@ namespace bond { namespace ext { namespace gRPC { /// return. void wait() { - bool shouldShutdown = !_isShutdownInProgress.test_and_set(); - if (shouldShutdown) - { - // borrow the current thread to clean up - for (auto& thread : _threads) + bond::detail::call_once( + _waitFlag, + [this] { - BOOST_ASSERT(thread.joinable()); - thread.join(); - } + for (auto& thread : _threads) + { + BOOST_ASSERT(thread.joinable()); + thread.join(); + } - _threads.clear(); - - _shutdownCompleted.set(); - } - else - { - // some other thread is performing clean up, so wait for it - _shutdownCompleted.wait(); - } + _threads.clear(); + }); } private: @@ -219,8 +212,7 @@ namespace bond { namespace ext { namespace gRPC { std::vector _threads; std::atomic_flag _isShutdownRequested = ATOMIC_FLAG_INIT; - std::atomic_flag _isShutdownInProgress = ATOMIC_FLAG_INIT; - bond::ext::detail::event _shutdownCompleted; + bond::detail::once_flag _waitFlag{}; }; } } } // namespace bond::ext::gRPC