refactor system stop

This commit is contained in:
2025-01-21 14:57:13 -06:00
parent f44bb6bcfc
commit 1c65f51ef4
2 changed files with 24 additions and 19 deletions

View File

@ -37,7 +37,7 @@ public:
~timeout() { disable(); } ~timeout() { disable(); }
private: private:
bool timeout_killed_; std::atomic<bool> timeout_killed_;
std::unique_ptr<std::thread> timeout_thread_; std::unique_ptr<std::thread> timeout_thread_;
std::mutex timeout_mutex_; std::mutex timeout_mutex_;
std::condition_variable timeout_notify_; std::condition_variable timeout_notify_;

View File

@ -27,7 +27,10 @@ namespace repertory {
timeout::timeout(std::function<void()> timeout_callback, timeout::timeout(std::function<void()> timeout_callback,
const std::chrono::system_clock::duration &duration) const std::chrono::system_clock::duration &duration)
: timeout_killed_(duration == 0s) { : timeout_killed_(duration == 0s) {
if (not timeout_killed_) { if (timeout_killed_) {
return;
}
timeout_thread_ = timeout_thread_ =
std::make_unique<std::thread>([this, duration, timeout_callback]() { std::make_unique<std::thread>([this, duration, timeout_callback]() {
unique_mutex_lock lock(timeout_mutex_); unique_mutex_lock lock(timeout_mutex_);
@ -38,17 +41,19 @@ timeout::timeout(std::function<void()> timeout_callback,
} }
} }
}); });
}
} }
void timeout::disable() { void timeout::disable() {
if (not timeout_killed_) { if (timeout_killed_) {
return;
}
timeout_killed_ = true; timeout_killed_ = true;
unique_mutex_lock lock(timeout_mutex_); unique_mutex_lock lock(timeout_mutex_);
timeout_notify_.notify_all(); timeout_notify_.notify_all();
lock.unlock(); lock.unlock();
timeout_thread_->join(); timeout_thread_->join();
timeout_thread_.reset(); timeout_thread_.reset();
}
} }
} // namespace repertory } // namespace repertory