From 586b49f4a808a75e19c7a4c71f0e99cfa5a73186 Mon Sep 17 00:00:00 2001 From: Green Sky Date: Sun, 22 Dec 2024 15:31:54 +0100 Subject: [PATCH] fix rare race condition in debug vid src --- src/debug_video_tap.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/debug_video_tap.cpp b/src/debug_video_tap.cpp index b6594e5..89935c7 100644 --- a/src/debug_video_tap.cpp +++ b/src/debug_video_tap.cpp @@ -85,6 +85,7 @@ struct DebugVideoTapSink : public FrameStream2SinkI { }; struct DebugVideoTestSource : public FrameStream2SourceI { + std::mutex _readers_mutex; std::vector>> _readers; std::atomic_bool _stop {false}; @@ -94,6 +95,9 @@ struct DebugVideoTestSource : public FrameStream2SourceI { std::cout << "DVTS: starting new test video source\n"; _thread = std::thread([this](void) { while (!_stop) { + std::this_thread::sleep_for(std::chrono::milliseconds(50)); + + std::lock_guard lg{_readers_mutex}; if (!_readers.empty()) { auto* surf = SDL_CreateSurface(960, 720, SDL_PIXELFORMAT_RGBA32); @@ -113,8 +117,6 @@ struct DebugVideoTestSource : public FrameStream2SourceI { SDL_DestroySurface(surf); } - - std::this_thread::sleep_for(std::chrono::milliseconds(50)); } }); } @@ -124,10 +126,12 @@ struct DebugVideoTestSource : public FrameStream2SourceI { } std::shared_ptr> subscribe(void) override { + std::lock_guard lg{_readers_mutex}; return _readers.emplace_back(std::make_shared>()); } bool unsubscribe(const std::shared_ptr>& sub) override { + std::lock_guard lg{_readers_mutex}; for (auto it = _readers.cbegin(); it != _readers.cend(); it++) { if (it->get() == sub.get()) { _readers.erase(it);