Fix crash issue 08/317308/2
authorChanggyu Choi <changyu.choi@samsung.com>
Mon, 30 Dec 2024 08:28:42 +0000 (17:28 +0900)
committerChanggyu Choi <changyu.choi@samsung.com>
Mon, 30 Dec 2024 08:41:19 +0000 (17:41 +0900)
memory allocation must not be performed at the end of the process.

Change-Id: I41163045f3ffa9d3addb5c3cb8aa4a5f65536d48
Signed-off-by: Changgyu Choi <changyu.choi@samsung.com>
src/rpc-port/cynara_thread.cc
src/rpc-port/cynara_thread.hh

index fbb2ee8faab7b0219b6c2cf56d1d459dedeea9c1..fe01cfb149b58f0bcd685b5f53549f547af64d75 100644 (file)
@@ -30,13 +30,20 @@ Job::Type Job::Do() {
   return cb_();
 }
 
+static CynaraThread* inst = new CynaraThread();
+
 CynaraThread& CynaraThread::GetInst() {
-  static CynaraThread* inst = new CynaraThread();
   return *inst;
 }
 
 CynaraThread::CynaraThread() {
-  thread_ = std::thread([this]() { ThreadRun(); });
+}
+
+void CynaraThread::Init() {
+  if (!disposed_)
+    return;
+
+  thread_.reset(new std::thread([this]() { ThreadRun(); }));
   disposed_ = false;
 }
 
@@ -54,7 +61,7 @@ void CynaraThread::Dispose() {
     done_ = true;
     cond_var_.notify_one();
   }
-  thread_.join();
+  thread_->join();
   disposed_ = true;
 }
 
@@ -73,6 +80,8 @@ void CynaraThread::ThreadRun() {
 
 void CynaraThread::Push(std::shared_ptr<Job> job) {
   std::lock_guard<std::mutex> lock(mutex_);
+  Init();
+
   queue_.push(std::move(job));
   cond_var_.notify_one();
 }
index acec34f2b95b1d6b5947ac5358bebdc685796e9d..97baca54b41c00b6ccb6422d3a65472bffddda74 100644 (file)
@@ -50,21 +50,22 @@ class Job {
 class CynaraThread {
  public:
   static CynaraThread& GetInst();
+  CynaraThread();
   CynaraThread(CynaraThread&) = delete;
   CynaraThread& operator=(CynaraThread&) = delete;
   ~CynaraThread();
 
   void Push(std::shared_ptr<Job> job);
+  void Init();
   void Dispose();
 
  private:
-  CynaraThread();
   void ThreadRun();
   std::shared_ptr<Job> WaitAndPop();
 
   bool disposed_ = true;
   std::atomic<bool> done_{false};
-  std::thread thread_;
+  std::unique_ptr<std::thread> thread_;
   std::queue<std::shared_ptr<Job>> queue_;
   mutable std::mutex mutex_;
   std::condition_variable cond_var_;