diff --git a/source/common/thread_local/thread_local_impl.cc b/source/common/thread_local/thread_local_impl.cc index 14bcf251bd5c6..6884aae42d9a7 100644 --- a/source/common/thread_local/thread_local_impl.cc +++ b/source/common/thread_local/thread_local_impl.cc @@ -98,15 +98,15 @@ void InstanceImpl::runOnAllThreads(Event::PostCb cb, Event::PostCb all_threads_c // all_threads_complete_cb method. Parallelism of main thread execution is being traded off // for programming simplicity here. cb(); - std::shared_ptr> worker_count = - std::make_shared>(registered_threads_.size()); + + std::shared_ptr cb_guard(new Event::PostCb(cb), + [this, all_threads_complete_cb](Event::PostCb* cb) { + main_thread_dispatcher_->post(all_threads_complete_cb); + delete cb; + }); + for (Event::Dispatcher& dispatcher : registered_threads_) { - dispatcher.post([this, worker_count, cb, all_threads_complete_cb]() -> void { - cb(); - if (--*worker_count == 0) { - main_thread_dispatcher_->post(all_threads_complete_cb); - } - }); + dispatcher.post([cb_guard]() -> void { (*cb_guard)(); }); } }