fix(progress): ensure timely exit in progress dtor

This commit is contained in:
Marcus Holland-Moritz 2024-01-03 13:02:24 +01:00
parent 8e4ecd6a9f
commit 10cbbb3d58
2 changed files with 7 additions and 4 deletions

View File

@ -148,7 +148,8 @@ class progress {
private: private:
void add_context(std::shared_ptr<context> const& ctx) const; void add_context(std::shared_ptr<context> const& ctx) const;
std::atomic<bool> running_; mutable std::mutex running_mx_;
bool running_;
mutable std::mutex mx_; mutable std::mutex mx_;
std::condition_variable cond_; std::condition_variable cond_;
std::shared_ptr<status_function_type> status_fun_; std::shared_ptr<status_function_type> status_fun_;

View File

@ -38,8 +38,7 @@ progress::progress(folly::Function<void(progress&, bool)>&& func,
#ifdef _WIN32 #ifdef _WIN32
::SetThreadPriority(::GetCurrentThread(), THREAD_PRIORITY_HIGHEST); ::SetThreadPriority(::GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
#endif #endif
std::mutex mx_thread; std::unique_lock lock(running_mx_);
std::unique_lock lock(mx_thread);
while (running_) { while (running_) {
func(*this, false); func(*this, false);
cond_.wait_for(lock, std::chrono::milliseconds(interval_ms)); cond_.wait_for(lock, std::chrono::milliseconds(interval_ms));
@ -50,7 +49,10 @@ progress::progress(folly::Function<void(progress&, bool)>&& func,
progress::~progress() noexcept { progress::~progress() noexcept {
try { try {
{
std::lock_guard lock(running_mx_);
running_ = false; running_ = false;
}
cond_.notify_all(); cond_.notify_all();
thread_.join(); thread_.join();
} catch (...) { } catch (...) {