Fix crash caused by multi threads during program termination

This commit is contained in:
dijunkun
2023-09-11 14:45:22 +08:00
parent 79c838629a
commit 0899fe2f1d
11 changed files with 199 additions and 122 deletions

View File

@@ -0,0 +1,24 @@
#include "thread_base.h"
#include "log.h"
ThreadBase::ThreadBase() {}
ThreadBase::~ThreadBase() {}
void ThreadBase::StartThread() {
if (!thread_) {
thread_ = std::make_unique<std::thread>(&ThreadBase::Run, this);
}
}
void ThreadBase::StopThread() {
if (thread_ && thread_->joinable()) {
thread_->join();
}
}
void ThreadBase::Run() {
while (Process()) {
}
}

26
src/thread/thread_base.h Normal file
View File

@@ -0,0 +1,26 @@
#ifndef _THREAD_BASE_H_
#define _THREAD_BASE_H_
#include <mutex>
#include <thread>
class ThreadBase {
public:
ThreadBase();
~ThreadBase();
public:
void StartThread();
void StopThread();
virtual bool Process() = 0;
private:
void Run();
private:
std::unique_ptr<std::thread> thread_ = nullptr;
bool start_ = false;
std::mutex mutex_;
};
#endif