[fix] fix crash due to rtp receivers destroy

This commit is contained in:
dijunkun
2025-02-06 17:08:26 +08:00
parent 1d85247785
commit 3accdf2192
8 changed files with 82 additions and 36 deletions

View File

@@ -2,23 +2,34 @@
#include "log.h"
ThreadBase::ThreadBase() {}
ThreadBase::ThreadBase()
: running_(false), pause_(false), period_(std::chrono::milliseconds(100)) {}
ThreadBase::~ThreadBase() {
if (!stop_) {
Stop();
}
}
ThreadBase::~ThreadBase() { Stop(); }
void ThreadBase::Start() {
{
std::lock_guard<std::mutex> lock(cv_mtx_);
if (running_) {
return; // Already running
}
running_ = true;
}
std::thread t(&ThreadBase::Run, this);
thread_ = std::move(t);
stop_ = false;
}
void ThreadBase::Stop() {
{
std::lock_guard<std::mutex> lock(cv_mtx_);
if (!running_) {
return; // Already stopped
}
running_ = false;
}
cv_.notify_all();
if (thread_.joinable()) {
stop_ = true;
thread_.join();
}
}
@@ -27,7 +38,17 @@ void ThreadBase::Pause() { pause_ = true; }
void ThreadBase::Resume() { pause_ = false; }
void ThreadBase::SetPeriod(std::chrono::milliseconds period) {
std::lock_guard<std::mutex> lock(cv_mtx_);
period_ = period;
}
void ThreadBase::Run() {
while (!stop_ && Process()) {
while (running_) {
std::unique_lock<std::mutex> lock(cv_mtx_);
cv_.wait_for(lock, period_, [this] { return !running_; });
if (running_) {
Process();
}
}
}

View File

@@ -2,6 +2,9 @@
#define _THREAD_BASE_H_
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <mutex>
#include <thread>
class ThreadBase {
@@ -16,6 +19,8 @@ class ThreadBase {
void Pause();
void Resume();
void SetPeriod(std::chrono::milliseconds period);
virtual bool Process() = 0;
private:
@@ -23,9 +28,13 @@ class ThreadBase {
private:
std::thread thread_;
std::chrono::milliseconds period_;
std::atomic<bool> stop_{false};
std::atomic<bool> pause_{false};
std::condition_variable cv_;
std::mutex cv_mtx_;
std::atomic<bool> running_;
std::atomic<bool> pause_;
};
#endif