C++11中std::thread線程實現(xiàn)暫停(掛起)功能
一、封裝Thread類
我們基于C++11中與平臺無關(guān)的線程類std::thread,封裝Thread類,并提供start()、stop()、pause()、resume()線程控制方法。
為了讓線程在暫停期間,處于休眠,不消耗CPU,我們使用C++11提供的鎖和條件變量來實現(xiàn)。
- std::mutex
- std::condition_variable
Thread.h
#ifndef THREAD_H #define THREAD_H #include <thread> #include <atomic> #include <mutex> #include <condition_variable> class Thread { public: ? ? Thread(); ? ? virtual ~Thread(); ? ? enum State ? ? { ? ? ? ? Stoped, ? ? ///<停止狀態(tài),包括從未啟動過和啟動后被停止 ? ? ? ? Running, ? ?///<運行狀態(tài) ? ? ? ? Paused ? ? ?///<暫停狀態(tài) ? ? }; ? ? State state() const; ? ? void start(); ? ? void stop(); ? ? void pause(); ? ? void resume(); protected: ? ? virtual void process() = 0; private: ? ? void run(); private: ? ? std::thread* _thread; ? ? std::mutex _mutex; ? ? std::condition_variable _condition; ? ? std::atomic_bool _pauseFlag; ? ///<暫停標識 ? ? std::atomic_bool _stopFlag; ? ///<停止標識 ? ? State _state; }; #endif // THREAD_H
Thread.cpp
#include "Thread.h" #include <iostream> using namespace std; Thread::Thread() ? ? : _thread(nullptr), ? ? ? _pauseFlag(false), ? ? ? _stopFlag(false), ? ? ? _state(Stoped) { } Thread::~Thread() { ? ? stop(); } Thread::State Thread::state() const { ? ? return _state; } void Thread::start() { ? ? if (_thread == nullptr) ? ? { ? ? ? ? _thread = new thread(&Thread::run, this); ? ? ? ? _pauseFlag = false; ? ? ? ? _stopFlag = false; ? ? ? ? _state = Running; ? ? } } void Thread::stop() { ? ? if (_thread != nullptr) ? ? { ? ? ? ? _pauseFlag = false; ? ? ? ? _stopFlag = true; ? ? ? ? _condition.notify_all(); ?// Notify one waiting thread, if there is one. ? ? ? ? _thread->join(); // wait for thread finished ? ? ? ? delete _thread; ? ? ? ? _thread = nullptr; ? ? ? ? _state = Stoped; ? ? } } void Thread::pause() { ? ? if (_thread != nullptr) ? ? { ? ? ? ? _pauseFlag = true; ? ? ? ? _state = Paused; ? ? } } void Thread::resume() { ? ? if (_thread != nullptr) ? ? { ? ? ? ? _pauseFlag = false; ? ? ? ? _condition.notify_all(); ? ? ? ? _state = Running; ? ? } } void Thread::run() { ? ? cout << "enter thread:" << this_thread::get_id() << endl; ? ? while (!_stopFlag) ? ? { ? ? ? ? process(); ? ? ? ? if (_pauseFlag) ? ? ? ? { ? ? ? ? ? ? unique_lock<mutex> locker(_mutex); ? ? ? ? ? ? while (_pauseFlag) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? _condition.wait(locker); // Unlock _mutex and wait to be notified ? ? ? ? ? ? } ? ? ? ? ? ? locker.unlock(); ? ? ? ? } ? ? } ? ? _pauseFlag = false; ? ? _stopFlag = false; ? ? cout << "exit thread:" << this_thread::get_id() << endl; }
二、測試代碼
main.cpp
#include <QCoreApplication> #include <iostream> #include "Thread.h" using namespace std; void mySleep(int s) { ? ? std::this_thread::sleep_for(std::chrono::duration<double>(s)); } class MyThread : public Thread { protected: ? ? virtual void process() override ? ? { ? ? ? ? cout << "do my something" << endl; ? ? ? ? mySleep(1); ? ? } }; int main(int argc, char *argv[]) { ? ? QCoreApplication a(argc, argv); ? ? MyThread thread; ? ? cout << "start thread" << endl; ? ? thread.start(); ? ? cout << "thread state:" << thread.state() << endl; ? ? mySleep(3); ? ? cout << "pause thread" << endl; ? ? thread.pause(); ? ? cout << "thread state:" << thread.state() << endl; ? ? mySleep(3); ? ? cout << "resume thread" << endl; ? ? thread.resume(); ? ? cout << "thread state:" << thread.state() << endl; ? ? mySleep(3); ? ? cout << "stop thread" << endl; ? ? thread.stop(); ? ? cout << "thread state:" << thread.state() << endl; ? ? mySleep(3); ? ? return a.exec(); }
運行結(jié)果:
到此這篇關(guān)于C++11中std::thread線程實現(xiàn)暫停(掛起)功能的文章就介紹到這了,更多相關(guān)C++11 std::thread線程暫停內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語言動態(tài)內(nèi)存管理malloc柔性數(shù)組示例詳解
這篇文章主要為大家介紹了C語言動態(tài)內(nèi)存管理malloc柔性數(shù)組示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10C語言數(shù)據(jù)類型與sizeof關(guān)鍵字
這篇文章主要介紹了C語言數(shù)據(jù)類型與sizeof關(guān)鍵字,C語言的數(shù)據(jù)類型包括基本類型、構(gòu)造類型、指針類型以及空類型,下文更多相關(guān)內(nèi)容需要的小伙伴可以參考一下2022-04-04c++ lambda捕獲this 導(dǎo)致多線程下類釋放后還在使用的錯誤問題
Lambda表達式是現(xiàn)代C++的一個語法糖,挺好用的。但是如果使用不當,會導(dǎo)致內(nèi)存泄露或潛在的崩潰問題,這里總結(jié)下c++ lambda捕獲this 導(dǎo)致多線程下類釋放后還在使用的錯誤問題,感興趣的朋友一起看看吧2023-02-02