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(); }
private:
bool timeout_killed_;
std::atomic<bool> timeout_killed_;
std::unique_ptr<std::thread> timeout_thread_;
std::mutex timeout_mutex_;
std::condition_variable timeout_notify_;

View File

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