From 06af73bc90feab48733da2f455f382bc2575114b Mon Sep 17 00:00:00 2001 From: jason416 Date: Fri, 24 May 2019 11:37:39 +0800 Subject: [PATCH] use timed_mutex. for all communication ordered, a simple mutex works fine --- src/client/binary_client.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/client/binary_client.cpp b/src/client/binary_client.cpp index 274f632b..8f1a7d46 100644 --- a/src/client/binary_client.cpp +++ b/src/client/binary_client.cpp @@ -83,8 +83,12 @@ class RequestCallback public: RequestCallback(const Common::Logger::SharedPtr & logger) : Logger(logger) - , lock(m) { + m.lock(); + } + ~RequestCallback() + { + m.unlock(); } void OnData(std::vector data, ResponseHeader h) @@ -92,12 +96,12 @@ class RequestCallback //std::cout << ToHexDump(data); Data = std::move(data); this->header = std::move(h); - doneEvent.notify_all(); + m.unlock(); } T WaitForData(std::chrono::milliseconds msec) { - if (doneEvent.wait_for(lock, msec) == std::cv_status::timeout) + if (m.try_lock_for(msec) == false) { throw std::runtime_error("Response timed out"); } T result; @@ -122,9 +126,7 @@ class RequestCallback Common::Logger::SharedPtr Logger; std::vector Data; ResponseHeader header; - std::mutex m; - std::unique_lock lock; - std::condition_variable doneEvent; + std::timed_mutex m; }; class CallbackThread @@ -883,6 +885,7 @@ class BinaryClient catch (std::exception & ex) { //Remove the callback on timeout + LOG_DEBUG(Logger, "binary_client | send exception: handle: {}", request.Header.RequestHandle); std::unique_lock lock(Mutex); Callbacks.erase(request.Header.RequestHandle); lock.unlock();