mirror of
https://github.com/kunkundi/crossdesk.git
synced 2025-10-27 04:35:34 +08:00
[fix] fix crash due to rtp receivers destroy
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user