diff --git a/repertory/librepertory/include/events/t_event_system.hpp b/repertory/librepertory/include/events/t_event_system.hpp index 45e3e93d..e3090714 100644 --- a/repertory/librepertory/include/events/t_event_system.hpp +++ b/repertory/librepertory/include/events/t_event_system.hpp @@ -134,13 +134,11 @@ private: const event_type &event) { std::deque> futures; recur_mutex_lock consumer_lock(consumer_mutex_); - if (event_consumers_.find(name) != event_consumers_.end()) { - for (auto *consumer : event_consumers_[name]) { - futures.emplace_back( - std::async(std::launch::async, [consumer, &event]() { - consumer->notify_event(event); - })); - } + for (auto *consumer : event_consumers_[name]) { + futures.emplace_back( + std::async(std::launch::async, [consumer, &event]() { + consumer->notify_event(event); + })); } while (not futures.empty()) { diff --git a/repertory/librepertory/src/utils/single_thread_service_base.cpp b/repertory/librepertory/src/utils/single_thread_service_base.cpp index 26d824b1..6e31a98e 100644 --- a/repertory/librepertory/src/utils/single_thread_service_base.cpp +++ b/repertory/librepertory/src/utils/single_thread_service_base.cpp @@ -38,33 +38,40 @@ void single_thread_service_base::notify_all() const { void single_thread_service_base::start() { mutex_lock lock(mtx_); - if (not thread_) { - stop_requested_ = false; - on_start(); - thread_ = std::make_unique([this]() { - event_system::instance().raise(service_name_); - while (not get_stop_requested()) { - service_function(); - } - }); + if (thread_) { + return; } + + stop_requested_ = false; + on_start(); + thread_ = std::make_unique([this]() { + event_system::instance().raise(service_name_); + while (not get_stop_requested()) { + service_function(); + } + }); } void single_thread_service_base::stop() { - if (thread_) { - event_system::instance().raise(service_name_); - unique_mutex_lock lock(mtx_); - if (thread_) { - stop_requested_ = true; - notify_.notify_all(); - lock.unlock(); - - thread_->join(); - thread_.reset(); - - on_stop(); - } - event_system::instance().raise(service_name_); + unique_mutex_lock lock(mtx_); + if (not thread_) { + return; } + + event_system::instance().raise(service_name_); + + std::unique_ptr thread{nullptr}; + std::swap(thread, thread_); + + stop_requested_ = true; + notify_.notify_all(); + lock.unlock(); + + thread->join(); + thread.reset(); + + on_stop(); + + event_system::instance().raise(service_name_); } } // namespace repertory