mirror of
https://github.com/zeromq/libzmq.git
synced 2024-12-27 23:51:04 +08:00
Problem: std::condition_variable can only be used with std::unique_lock<std::mutex>, leading to two mutexes used in condition_variable_t
Solution: use std::condition_variable_any instead
This commit is contained in:
parent
119a258504
commit
d7e1cf3eb0
@ -83,7 +83,6 @@ class condition_variable_t
|
|||||||
|
|
||||||
#if _SUPPORT_CONDITION_VARIABLE || defined(ZMQ_HAVE_WINDOWS_TARGET_XP)
|
#if _SUPPORT_CONDITION_VARIABLE || defined(ZMQ_HAVE_WINDOWS_TARGET_XP)
|
||||||
#include <condition_variable>
|
#include <condition_variable>
|
||||||
#include <mutex>
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace zmq
|
namespace zmq
|
||||||
@ -132,32 +131,28 @@ class condition_variable_t
|
|||||||
|
|
||||||
inline int wait (mutex_t *mutex_, int timeout_)
|
inline int wait (mutex_t *mutex_, int timeout_)
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lck (_mtx); // lock mtx
|
// this assumes that the mutex mutex_ has been locked by the caller
|
||||||
mutex_->unlock (); // unlock mutex_
|
|
||||||
int res = 0;
|
int res = 0;
|
||||||
if (timeout_ == -1) {
|
if (timeout_ == -1) {
|
||||||
_cv.wait (
|
_cv.wait (
|
||||||
lck); // unlock mtx and wait cv.notify_all(), lock mtx after cv.notify_all()
|
*mutex_); // unlock mtx and wait cv.notify_all(), lock mtx after cv.notify_all()
|
||||||
} else if (_cv.wait_for (lck, std::chrono::milliseconds (timeout_))
|
} else if (_cv.wait_for (*mutex_, std::chrono::milliseconds (timeout_))
|
||||||
== std::cv_status::timeout) {
|
== std::cv_status::timeout) {
|
||||||
// time expired
|
// time expired
|
||||||
errno = EAGAIN;
|
errno = EAGAIN;
|
||||||
res = -1;
|
res = -1;
|
||||||
}
|
}
|
||||||
lck.unlock (); // unlock mtx
|
|
||||||
mutex_->lock (); // lock mutex_
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void broadcast ()
|
inline void broadcast ()
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lck (_mtx); // lock mtx
|
// this assumes that the mutex associated with _cv has been locked by the caller
|
||||||
_cv.notify_all ();
|
_cv.notify_all ();
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::condition_variable _cv;
|
std::condition_variable_any _cv;
|
||||||
std::mutex _mtx;
|
|
||||||
|
|
||||||
// Disable copy construction and assignment.
|
// Disable copy construction and assignment.
|
||||||
condition_variable_t (const condition_variable_t &);
|
condition_variable_t (const condition_variable_t &);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user