From f43d6563a5a8099df68f24a6ca909d85cdf0fad8 Mon Sep 17 00:00:00 2001 From: agan Date: Sun, 29 Mar 2020 20:28:35 +0800 Subject: [PATCH 1/2] mac os compiled error with __suseconds_t --- src/redisclient/impl/redisclientimpl.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/redisclient/impl/redisclientimpl.cpp b/src/redisclient/impl/redisclientimpl.cpp index fc43856..b6ee493 100644 --- a/src/redisclient/impl/redisclientimpl.cpp +++ b/src/redisclient/impl/redisclientimpl.cpp @@ -12,6 +12,10 @@ #include "redisclientimpl.h" +#ifdef __APPLE__ +typedef suseconds_t __suseconds_t; +#endif + namespace { static const char crlf[] = {'\r', '\n'}; From edf46e4a0db49a99ef511a01e13593b90bbd5604 Mon Sep 17 00:00:00 2001 From: agan Date: Sun, 29 Mar 2020 20:31:05 +0800 Subject: [PATCH 2/2] Multiple thread may excute asyncWrite at the same time. 1. Thread A request quite a lot RedisAsyncClient::command, all the command post to RedisClientImpl::strand. 2. Thread B execute commands in strand one by one. The first asyncWrite swap dataWrited and dataQueued, so other doAsyncCommand won't enter asyncWrite. 3. the first asyncWrite call ends with a boost::asio::async_write, and left a later asyncWrite as handler. 4. boost::asio::async_write complete, thread C excute the handler asyncWrite, clear dataWrited. 5. Now thread B execute doAsyncCommand, and find dataWrited was empty, so it enters asyncWrite concurrently! --- src/redisclient/impl/redisclientimpl.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/redisclient/impl/redisclientimpl.cpp b/src/redisclient/impl/redisclientimpl.cpp index b6ee493..3470fa6 100644 --- a/src/redisclient/impl/redisclientimpl.cpp +++ b/src/redisclient/impl/redisclientimpl.cpp @@ -360,8 +360,9 @@ void RedisClientImpl::asyncWrite(const boost::system::error_code &ec, size_t) std::swap(dataQueued, dataWrited); boost::asio::async_write(socket, buffers, - std::bind(&RedisClientImpl::asyncWrite, shared_from_this(), - std::placeholders::_1, std::placeholders::_2)); + strand.wrap( + std::bind(&RedisClientImpl::asyncWrite, shared_from_this(), + std::placeholders::_1, std::placeholders::_2))); } }