From f5186193604e10c92669d9c4a2cbfd4f1f774d77 Mon Sep 17 00:00:00 2001 From: philippegorley <philippe.gorley@savoirfairelinux.com> Date: Wed, 4 Jul 2018 16:56:01 -0400 Subject: [PATCH] thread: wait until condition is fulfilled Change-Id: I3a8f56aa4d93f73bd1290534e682f7d5f5025c5a --- src/threadloop.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/threadloop.h b/src/threadloop.h index ff6d6111b0..a3269f7213 100644 --- a/src/threadloop.h +++ b/src/threadloop.h @@ -114,6 +114,17 @@ public: return cv_.wait_for(lk, rel_time, [this, pred]{ return isStopping() || pred(); }); } + template <typename Pred> + void + wait(Pred&& pred) + { + if (std::this_thread::get_id() != get_id()) + throw std::runtime_error("Can not call wait outside thread context"); + + std::unique_lock<std::mutex> lk(mutex_); + cv_.wait(lk, [this, p = std::move(pred)]{ return isStopping() || p(); }); + } + private: std::mutex mutex_; std::condition_variable cv_; -- GitLab