mirror of
https://github.com/kunkundi/crossdesk.git
synced 2025-10-27 12:45:35 +08:00
Fix crash caused by multi threads during program termination
This commit is contained in:
24
src/thread/thread_base.cpp
Normal file
24
src/thread/thread_base.cpp
Normal 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
26
src/thread/thread_base.h
Normal 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
|
||||
Reference in New Issue
Block a user