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:
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_;
std::condition_variable cond_;
std::shared_ptr<status_function_type> status_fun_;

View File

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