From 3739692d07d7844ce5a6b731c7f893a479fb3089 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Mon, 11 Feb 2019 15:21:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Ddbpool=E5=86=85=E5=AD=98?= =?UTF-8?q?=E6=B3=84=E6=BC=8F=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cpp/dbpool.cc | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/cpp/dbpool.cc b/cpp/dbpool.cc index a7b2000..21b427f 100644 --- a/cpp/dbpool.cc +++ b/cpp/dbpool.cc @@ -75,6 +75,33 @@ namespace f8 work_thread_ = new std::thread(&DBThread::WorkThreadProc, this); } + void UnInit() + { + terminated_ = true; + work_thread_->join(); + delete work_thread_; + work_thread_ = nullptr; + + if (last_query_) { + delete last_query_; + last_query_ = nullptr; + } + + if (last_conn_) { + delete last_conn_; + last_conn_ = nullptr; + } + + delete msg_mutex_; + msg_mutex_ = nullptr; + + delete loop_cond_; + loop_cond_ = nullptr; + + delete loop_mutex_; + loop_mutex_ = nullptr; + } + void AddAsyncQuery(AsyncQueryNode* p) { std::unique_lock lk(*loop_mutex_); @@ -94,7 +121,7 @@ namespace f8 void WorkThreadProc() { - while (true) { + while (!terminated_) { CheckDB(); ProcessMsg(); WaitLoopCond(); @@ -266,6 +293,7 @@ namespace f8 int exec_async_query_msgid = 0; private: + volatile bool terminated_ = false; std::mutex *loop_mutex_ = nullptr; std::condition_variable *loop_cond_ = nullptr; @@ -296,6 +324,14 @@ namespace f8 exec_async_query_msgid = MsgQueue::Instance()->AllocIMMsgId(); } + void UnInit() + { + for (auto& db_thread : db_thread_pool) { + db_thread->UnInit(); + delete db_thread; + } + } + void SetThreadNum(int thread_num) { assert(thread_num > 0); @@ -424,6 +460,7 @@ namespace f8 void DBPool::UnInit() { + impl_->UnInit(); delete impl_; impl_ = nullptr; }