From 2d4b871401ace5a54eaff1cbfacd008c586ad928 Mon Sep 17 00:00:00 2001
From: zhangmeng <775834166@qq.com>
Date: 星期一, 05 八月 2019 10:32:51 +0800
Subject: [PATCH] fix thread quit

---
 csrc/buz/recorder.hpp |    2 ++
 csrc/buz/recorder.cpp |   35 +++++++++++++++++++++++------------
 2 files changed, 25 insertions(+), 12 deletions(-)

diff --git a/csrc/buz/recorder.cpp b/csrc/buz/recorder.cpp
index 2cb3b12..3615729 100644
--- a/csrc/buz/recorder.cpp
+++ b/csrc/buz/recorder.cpp
@@ -31,22 +31,32 @@
         ,file_frame_index_(-1)
         ,file_path_("")
         ,func_rec_info_(nullptr)
+        ,thrd_(nullptr)
         {
             // logIt("RECODER ID: %s", id_.c_str());
         }
 
         Recorder::~Recorder(){
-            if (out_ && !stop_recorder_.load()){
-                stop_recorder_.store(true);
-                cv_.notify_one();
-                while(stop_recorder_.load()){
-                    logIt("REC WAIT TO QUIT ID %s", id_.c_str());
-                    sleep(1);
-                } 
-            }else if (stop_recorder_.load()){
-                logIt("REC NORMAL QUIT");
-            }
+            // if (out_){
+            //     stop_recorder_.store(true);
+            //     cv_.notify_one();
+            //     int try_cnt = 0;
+            //     while(stop_recorder_.load()){
+            //         logIt("REC WAIT TO QUIT ID %s", id_.c_str());
+            //         if (try_cnt > 100){
+            //             logIt("REC TRY %d TIMES TO QUIT..., BREAK!!!", try_cnt);
+            //             break;
+            //         }
+            //         try_cnt++;
+            //         sleep(1);
+            //     } 
+            // }else if (stop_recorder_.load()){
+            //     logIt("REC NORMAL QUIT");
+            // }
             
+            stop_recorder_.store(true);
+            thrd_->join();
+            logIt("REC THREAD JOINED, QUIT!!!");
         }
 
         int Recorder::init_writer(){
@@ -190,9 +200,10 @@
 
             logIt("minduration %d maxduration %d curduration %d", minduration, maxduration, end_frame);    
 
-            std::thread([&]{
+            thrd_.reset(new std::thread([&]{
                 run_thread();
-            }).detach();
+            }));
+            //.detach();
 
             return 0;
         }
diff --git a/csrc/buz/recorder.hpp b/csrc/buz/recorder.hpp
index 483404e..7f12d2e 100644
--- a/csrc/buz/recorder.hpp
+++ b/csrc/buz/recorder.hpp
@@ -64,6 +64,8 @@
                 std::mutex              mutex_pkt_;
      		    std::condition_variable cv_;
 
+                std::unique_ptr<std::thread> thrd_;
+                
                 std::string             dir_;
                 std::string             id_;
 

--
Gitblit v1.8.0