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,28 +27,33 @@ namespace repertory {
timeout::timeout(std::function<void()> timeout_callback,
const std::chrono::system_clock::duration &duration)
: timeout_killed_(duration == 0s) {
if (not timeout_killed_) {
timeout_thread_ =
std::make_unique<std::thread>([this, duration, timeout_callback]() {
unique_mutex_lock lock(timeout_mutex_);
if (not timeout_killed_) {
timeout_notify_.wait_for(lock, duration);
if (not timeout_killed_) {
timeout_callback();
}
}
});
if (timeout_killed_) {
return;
}
timeout_thread_ =
std::make_unique<std::thread>([this, duration, timeout_callback]() {
unique_mutex_lock lock(timeout_mutex_);
if (not timeout_killed_) {
timeout_notify_.wait_for(lock, duration);
if (not timeout_killed_) {
timeout_callback();
}
}
});
}
void timeout::disable() {
if (not timeout_killed_) {
timeout_killed_ = true;
unique_mutex_lock lock(timeout_mutex_);
timeout_notify_.notify_all();
lock.unlock();
timeout_thread_->join();
timeout_thread_.reset();
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