mirror of
https://github.com/kunkundi/crossdesk.git
synced 2025-10-27 04:35:34 +08:00
1.Use std::move to initialize std::thread; 2.Fix thread cannot exit error
This commit is contained in:
@@ -4,21 +4,22 @@
|
||||
|
||||
ThreadBase::ThreadBase() {}
|
||||
|
||||
ThreadBase::~ThreadBase() { Stop(); }
|
||||
ThreadBase::~ThreadBase() {
|
||||
if (!stop_) {
|
||||
Stop();
|
||||
}
|
||||
}
|
||||
|
||||
void ThreadBase::Start() {
|
||||
if (!thread_) {
|
||||
thread_ = std::make_unique<std::thread>(&ThreadBase::Run, this);
|
||||
}
|
||||
|
||||
std::thread t(&ThreadBase::Run, this);
|
||||
thread_ = std::move(t);
|
||||
stop_ = false;
|
||||
}
|
||||
|
||||
void ThreadBase::Stop() {
|
||||
stop_ = true;
|
||||
|
||||
if (thread_ && thread_->joinable()) {
|
||||
thread_->join();
|
||||
if (thread_.joinable()) {
|
||||
stop_ = true;
|
||||
thread_.join();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
class ThreadBase {
|
||||
public:
|
||||
ThreadBase();
|
||||
~ThreadBase();
|
||||
virtual ~ThreadBase();
|
||||
|
||||
public:
|
||||
void Start();
|
||||
@@ -22,7 +22,7 @@ class ThreadBase {
|
||||
void Run();
|
||||
|
||||
private:
|
||||
std::unique_ptr<std::thread> thread_ = nullptr;
|
||||
std::thread thread_;
|
||||
|
||||
std::atomic<bool> stop_{false};
|
||||
std::atomic<bool> pause_{false};
|
||||
|
||||
Reference in New Issue
Block a user