Start thread after created when use ThreadBase

This commit is contained in:
dijunkun
2023-09-11 15:15:59 +08:00
parent 0899fe2f1d
commit f52142fc00
7 changed files with 21 additions and 57 deletions

View File

@@ -6,19 +6,27 @@ ThreadBase::ThreadBase() {}
ThreadBase::~ThreadBase() {}
void ThreadBase::StartThread() {
void ThreadBase::Start() {
if (!thread_) {
thread_ = std::make_unique<std::thread>(&ThreadBase::Run, this);
}
stop_ = false;
}
void ThreadBase::StopThread() {
void ThreadBase::Stop() {
stop_ = true;
if (thread_ && thread_->joinable()) {
thread_->join();
}
}
void ThreadBase::Pause() { pause_ = true; }
void ThreadBase::Resume() { pause_ = false; }
void ThreadBase::Run() {
while (Process()) {
while (!stop_ && Process()) {
}
}

View File

@@ -1,7 +1,7 @@
#ifndef _THREAD_BASE_H_
#define _THREAD_BASE_H_
#include <mutex>
#include <atomic>
#include <thread>
class ThreadBase {
@@ -10,8 +10,12 @@ class ThreadBase {
~ThreadBase();
public:
void StartThread();
void StopThread();
void Start();
void Stop();
void Pause();
void Resume();
virtual bool Process() = 0;
private:
@@ -19,8 +23,9 @@ class ThreadBase {
private:
std::unique_ptr<std::thread> thread_ = nullptr;
bool start_ = false;
std::mutex mutex_;
std::atomic<bool> stop_ = false;
std::atomic<bool> pause_ = false;
};
#endif