refactor system stop
This commit is contained in:
@ -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_;
|
||||||
|
@ -27,28 +27,33 @@ 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_) {
|
||||||
timeout_thread_ =
|
return;
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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() {
|
void timeout::disable() {
|
||||||
if (not timeout_killed_) {
|
if (timeout_killed_) {
|
||||||
timeout_killed_ = true;
|
return;
|
||||||
unique_mutex_lock lock(timeout_mutex_);
|
|
||||||
timeout_notify_.notify_all();
|
|
||||||
lock.unlock();
|
|
||||||
timeout_thread_->join();
|
|
||||||
timeout_thread_.reset();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
timeout_killed_ = true;
|
||||||
|
unique_mutex_lock lock(timeout_mutex_);
|
||||||
|
timeout_notify_.notify_all();
|
||||||
|
lock.unlock();
|
||||||
|
|
||||||
|
timeout_thread_->join();
|
||||||
|
timeout_thread_.reset();
|
||||||
}
|
}
|
||||||
} // namespace repertory
|
} // namespace repertory
|
||||||
|
Reference in New Issue
Block a user