From 0d1fd33446c6dda16e8ecb5a0ad9b0e42c44deee Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Tue, 28 May 2019 19:07:47 +0800 Subject: [PATCH] =?UTF-8?q?=E5=86=85=E5=AD=98=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cpp/httpclientpool.cc | 47 ++++++++++++++++++++++++++++++++++++++++++- cpp/netmsghandler.h | 10 +++++++++ cpp/tglog.cc | 5 ++++- cpp/utils.cc | 1 + 4 files changed, 61 insertions(+), 2 deletions(-) diff --git a/cpp/httpclientpool.cc b/cpp/httpclientpool.cc index 2c2883c..6bc6097 100644 --- a/cpp/httpclientpool.cc +++ b/cpp/httpclientpool.cc @@ -62,6 +62,39 @@ namespace f8 work_thread_ = new std::thread(&HttpThread::WorkThreadProc, this); } + void UnInit() + { + terminated = true; + loop_cond_->notify_all(); + work_thread_->join(); + delete work_thread_; + work_thread_ = nullptr; + loop_mutex_->lock(); + if (!work_node_) { + work_node_ = top_node_; + top_node_ = nullptr; + bot_node_ = nullptr; + } + while (work_node_) { + AsyncHttpNode* pdelnode = work_node_; + work_node_ = work_node_->nextnode; + if (!work_node_) { + work_node_ = top_node_; + top_node_ = nullptr; + bot_node_ = nullptr; + } + delete pdelnode; + } + loop_mutex_->unlock(); + + delete loop_cond_; + loop_cond_ = nullptr; + delete loop_mutex_; + loop_mutex_ = nullptr; + delete msg_mutex_; + msg_mutex_ = nullptr; + } + void AddAsyncHttp(AsyncHttpNode* p) { std::unique_lock lk(*loop_mutex_); @@ -81,7 +114,7 @@ namespace f8 void WorkThreadProc() { - while (true) { + while (!terminated) { ProcessMsg(); WaitLoopCond(); } @@ -189,6 +222,7 @@ namespace f8 int exec_async_http_msgid = 0; private: + volatile bool terminated = false; std::mutex *loop_mutex_ = nullptr; std::condition_variable *loop_cond_ = nullptr; @@ -209,6 +243,16 @@ namespace f8 exec_async_http_msgid = MsgQueue::Instance()->AllocIMMsgId(); } + void UnInit() + { + for (auto& itr : http_thread_pool) { + HttpThread* thread = itr; + thread->UnInit(); + delete thread; + } + http_thread_pool.clear(); + } + void SetThreadNum(int thread_num) { assert(thread_num > 0); @@ -334,6 +378,7 @@ namespace f8 void HttpClientPool::UnInit() { + impl_->UnInit(); delete impl_; impl_ = nullptr; } diff --git a/cpp/netmsghandler.h b/cpp/netmsghandler.h index 35d53a0..a1b7777 100644 --- a/cpp/netmsghandler.h +++ b/cpp/netmsghandler.h @@ -22,6 +22,16 @@ namespace f8 struct NetMsgHandlerObject { NetMsgHandler* handlers[MAX_MSG_ID] = { nullptr }; + + ~NetMsgHandlerObject() + { + for (size_t i = 0; i < MAX_MSG_ID; ++i) { + if (handlers[i]) { + delete handlers[i]; + handlers[i] = nullptr; + } + } + } }; template diff --git a/cpp/tglog.cc b/cpp/tglog.cc index 4d412d7..d543f0a 100644 --- a/cpp/tglog.cc +++ b/cpp/tglog.cc @@ -27,7 +27,7 @@ namespace f8 bool is_poly_log = false; std::thread* save_thread = nullptr; - bool save_thread_shutdown = false; + volatile bool save_thread_shutdown = false; std::mutex msg_mutex; TGLogMsgNode* top_node = nullptr; @@ -44,6 +44,9 @@ namespace f8 virtual ~TGLogImpl() { + save_thread_shutdown = true; + save_thread->join(); + delete save_thread; delete save_cond_mutex; save_cond_mutex = nullptr; delete save_cond; diff --git a/cpp/utils.cc b/cpp/utils.cc index b972bdc..8575284 100644 --- a/cpp/utils.cc +++ b/cpp/utils.cc @@ -209,6 +209,7 @@ namespace f8 google::protobuf::Message* msg = prototype->New(); JsonToMessage(*p, msg); push_back_func(msg); + delete msg; } return true; }