From fa99fea4cb8780f2b52c3a7457f74448142f2ec8 Mon Sep 17 00:00:00 2001 From: zhonghong Date: Tue, 16 Jan 2024 11:16:44 +0800 Subject: [PATCH] update sample --- examples/c/window.hpp | 26 ++++++++++++++++++-------- examples/cpp/window.hpp | 26 ++++++++++++++++++-------- 2 files changed, 36 insertions(+), 16 deletions(-) diff --git a/examples/c/window.hpp b/examples/c/window.hpp index 090de27f..66ec9c57 100644 --- a/examples/c/window.hpp +++ b/examples/c/window.hpp @@ -60,7 +60,15 @@ class Window { public: // create a window with the specified name, width and height Window(std::string name, int width, int height, RenderType renderType_ = RENDER_SINGLE) - : name_(name), width_(width), height_(height), windowClose_(false), key_(-1), showInfo_(false), renderType_(renderType_), alpha_(0.6) { + : name_(name), + width_(width), + height_(height), + windowClose_(false), + threadExit_(false), + key_(-1), + showInfo_(false), + renderType_(renderType_), + alpha_(0.6) { processThread_ = std::thread(&Window::processFrames, this); #ifndef __APPLE__ renderThread_ = std::thread(&Window::renderMats, this); @@ -107,6 +115,7 @@ class Window { return; } windowClose_ = true; + threadExit_ = true; srcFramesCv_.notify_all(); processThread_.join(); #ifndef __APPLE__ @@ -132,7 +141,7 @@ class Window { #ifdef __APPLE__ render(); #endif - return !windowClose_; + return !threadExit_; } // set show frame info @@ -157,6 +166,7 @@ class Window { int width_; int height_; bool windowClose_; + bool threadExit_; bool showInfo_; float alpha_; @@ -190,11 +200,11 @@ class Window { uint32_t width; uint32_t height; uint8_t pixelAvailableBitSize; - while(!windowClose_) { + while(!threadExit_) { { std::unique_lock lk(srcFramesMtx_); - srcFramesCv_.wait(lk, [this] { return !srcFrames_.empty() || windowClose_; }); - if(windowClose_) { + srcFramesCv_.wait(lk, [this] { return !srcFrames_.empty() || threadExit_; }); + if(threadExit_) { break; } frames = srcFrames_; @@ -376,7 +386,7 @@ class Window { keyCv_.notify_all(); if(key == ESC_KEY) { - windowClose_ = true; + threadExit_ = true; srcFramesCv_.notify_all(); } } @@ -492,9 +502,9 @@ class Window { cv::namedWindow(name_, cv::WINDOW_NORMAL | cv::WINDOW_KEEPRATIO); cv::resizeWindow(name_, width_, height_); - while(!windowClose_) { + while(!threadExit_) { render(); } } #endif -}; \ No newline at end of file +}; diff --git a/examples/cpp/window.hpp b/examples/cpp/window.hpp index 052b1f8c..99a7807e 100644 --- a/examples/cpp/window.hpp +++ b/examples/cpp/window.hpp @@ -38,7 +38,15 @@ class Window { public: // create a window with the specified name, width and height Window(std::string name, int width, int height, RenderType renderType_ = RENDER_SINGLE) - : name_(name), width_(width), height_(height), windowClose_(false), key_(-1), showInfo_(false), renderType_(renderType_), alpha_(0.6) { + : name_(name), + width_(width), + height_(height), + windowClose_(false), + threadExit_(false), + key_(-1), + showInfo_(false), + renderType_(renderType_), + alpha_(0.6) { processThread_ = std::thread(&Window::processFrames, this); #ifndef __APPLE__ renderThread_ = std::thread(&Window::renderMats, this); @@ -78,6 +86,7 @@ class Window { return; } windowClose_ = true; + threadExit_ = true; srcFramesCv_.notify_all(); processThread_.join(); #ifndef __APPLE__ @@ -99,7 +108,7 @@ class Window { #ifdef __APPLE__ render(); #endif - return !windowClose_; + return !threadExit_; } // set show frame info @@ -124,6 +133,7 @@ class Window { int width_; int height_; bool windowClose_; + bool threadExit_; bool showInfo_; float alpha_; @@ -149,11 +159,11 @@ class Window { cv::Mat imuMat; cv::Mat rstMat; std::vector> frames; - while(!windowClose_) { + while(!threadExit_) { { std::unique_lock lk(srcFramesMtx_); - srcFramesCv_.wait(lk, [this] { return !srcFrames_.empty() || windowClose_; }); - if(windowClose_) { + srcFramesCv_.wait(lk, [this] { return !srcFrames_.empty() || threadExit_; }); + if(threadExit_) { break; } frames = srcFrames_; @@ -323,7 +333,7 @@ class Window { keyCv_.notify_all(); if(key == ESC_KEY) { - windowClose_ = true; + threadExit_ = true; srcFramesCv_.notify_all(); } } @@ -439,9 +449,9 @@ class Window { cv::namedWindow(name_, cv::WINDOW_NORMAL | cv::WINDOW_KEEPRATIO); cv::resizeWindow(name_, width_, height_); - while(!windowClose_) { + while(!threadExit_) { render(); } } #endif -}; \ No newline at end of file +};