From cf05ea3d9f43e4e84d621e1f9d54cbef552b6e2b Mon Sep 17 00:00:00 2001 From: lichao <lichao@aiotlink.com> Date: 星期二, 18 五月 2021 16:53:28 +0800 Subject: [PATCH] fix center init mutex. --- src/bh_api.cpp | 31 +++++++++++++++++++++---------- 1 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/bh_api.cpp b/src/bh_api.cpp index c9ceb20..b37eaae 100644 --- a/src/bh_api.cpp +++ b/src/bh_api.cpp @@ -30,16 +30,21 @@ } std::unique_ptr<TopicNode> &ProcNodePtr() { - static bool init = GlobalInit(BHomeShm()); - auto InitLog = []() { - auto id = GetProcExe(); - char path[200] = {0}; - sprintf(path, "/tmp/bhshmq_node_%s.log", id.c_str()); - ns_log::AddLog(path); - return true; - }; - static bool init_log = InitLog(); - static std::unique_ptr<TopicNode> ptr(new TopicNode(BHomeShm())); + static std::mutex mtx; + std::lock_guard<std::mutex> lk(mtx); + + static std::unique_ptr<TopicNode> ptr; + if (!ptr && GlobalInit(BHomeShm())) { + auto InitLog = []() { + auto id = GetProcExe(); + char path[200] = {0}; + sprintf(path, "/opt/vasystem/valog/bhshmq_node_%s.log", id.c_str()); + ns_log::AddLog(path); + return true; + }; + static bool init_log = InitLog(); + ptr.reset(new TopicNode(BHomeShm())); + } return ptr; } TopicNode &ProcNode() @@ -114,6 +119,12 @@ return false; } MsgOut msg_reply; + auto &ptr = ProcNodePtr(); + if (!ptr) { + SetLastError(eNotFound, "center not started."); + return 0; + } + return (ProcNode().*mfunc)(input, msg_reply, timeout_ms) && PackOutput(msg_reply, reply, reply_len); } -- Gitblit v1.8.0