From d7e1cf3eb0a5616634391eeb0d721fd6444e59dc Mon Sep 17 00:00:00 2001 From: Simon Giesecke Date: Mon, 11 Feb 2019 05:23:29 -0500 Subject: [PATCH] Problem: std::condition_variable can only be used with std::unique_lock, leading to two mutexes used in condition_variable_t Solution: use std::condition_variable_any instead --- src/condition_variable.hpp | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/condition_variable.hpp b/src/condition_variable.hpp index e5c72243..16502b94 100644 --- a/src/condition_variable.hpp +++ b/src/condition_variable.hpp @@ -83,7 +83,6 @@ class condition_variable_t #if _SUPPORT_CONDITION_VARIABLE || defined(ZMQ_HAVE_WINDOWS_TARGET_XP) #include -#include #endif namespace zmq @@ -132,32 +131,28 @@ class condition_variable_t inline int wait (mutex_t *mutex_, int timeout_) { - std::unique_lock lck (_mtx); // lock mtx - mutex_->unlock (); // unlock mutex_ + // this assumes that the mutex mutex_ has been locked by the caller int res = 0; if (timeout_ == -1) { _cv.wait ( - lck); // unlock mtx and wait cv.notify_all(), lock mtx after cv.notify_all() - } else if (_cv.wait_for (lck, std::chrono::milliseconds (timeout_)) + *mutex_); // unlock mtx and wait cv.notify_all(), lock mtx after cv.notify_all() + } else if (_cv.wait_for (*mutex_, std::chrono::milliseconds (timeout_)) == std::cv_status::timeout) { // time expired errno = EAGAIN; res = -1; } - lck.unlock (); // unlock mtx - mutex_->lock (); // lock mutex_ return res; } inline void broadcast () { - std::unique_lock lck (_mtx); // lock mtx + // this assumes that the mutex associated with _cv has been locked by the caller _cv.notify_all (); } private: - std::condition_variable _cv; - std::mutex _mtx; + std::condition_variable_any _cv; // Disable copy construction and assignment. condition_variable_t (const condition_variable_t &);