mirror of
https://github.com/kunkundi/crossdesk.git
synced 2025-10-27 04:35:34 +08:00
Start thread after created when use ThreadBase
This commit is contained in:
@@ -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()) {
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user