Skip to content

Commit

Permalink
use timed_mutex. for all communication ordered, a simple mutex works …
Browse files Browse the repository at this point in the history
…fine
  • Loading branch information
jason416 committed May 24, 2019
1 parent 2f2c886 commit 06af73b
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/client/binary_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,21 +83,25 @@ class RequestCallback
public:
RequestCallback(const Common::Logger::SharedPtr & logger)
: Logger(logger)
, lock(m)
{
m.lock();
}
~RequestCallback()
{
m.unlock();
}

void OnData(std::vector<char> data, ResponseHeader h)
{
//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;
Expand All @@ -122,9 +126,7 @@ class RequestCallback
Common::Logger::SharedPtr Logger;
std::vector<char> Data;
ResponseHeader header;
std::mutex m;
std::unique_lock<std::mutex> lock;
std::condition_variable doneEvent;
std::timed_mutex m;
};

class CallbackThread
Expand Down Expand Up @@ -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<std::mutex> lock(Mutex);
Callbacks.erase(request.Header.RequestHandle);
lock.unlock();
Expand Down

2 comments on commit 06af73b

@jason416
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for @codersmurf, issue 299. This patch was done by codersmurf. Fix many timed out exception even though client got response from server. After all there is only a send, receive thread, this patch can be more reliable to keep sync between Sender and Receiver thread.

@codersmurf
Copy link

@codersmurf codersmurf commented on 06af73b May 24, 2019 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.