C++11中std::thread線程實(shí)現(xiàn)暫停(掛起)功能
一、封裝Thread類
我們基于C++11中與平臺(tái)無關(guān)的線程類std::thread,封裝Thread類,并提供start()、stop()、pause()、resume()線程控制方法。
為了讓線程在暫停期間,處于休眠,不消耗CPU,我們使用C++11提供的鎖和條件變量來實(shí)現(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, ? ? ///<停止?fàn)顟B(tài),包括從未啟動(dòng)過和啟動(dòng)后被停止 ? ? ? ? Running, ? ?///<運(yùn)行狀態(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; ? ///<暫停標(biāo)識(shí) ? ? std::atomic_bool _stopFlag; ? ///<停止標(biāo)識(shí) ? ? 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(); }
運(yùn)行結(jié)果:
到此這篇關(guān)于C++11中std::thread線程實(shí)現(xiàn)暫停(掛起)功能的文章就介紹到這了,更多相關(guān)C++11 std::thread線程暫停內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語言動(dòng)態(tài)內(nèi)存管理malloc柔性數(shù)組示例詳解
這篇文章主要為大家介紹了C語言動(dòng)態(tài)內(nèi)存管理malloc柔性數(shù)組示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪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)致多線程下類釋放后還在使用的錯(cuò)誤問題
Lambda表達(dá)式是現(xiàn)代C++的一個(gè)語法糖,挺好用的。但是如果使用不當(dāng),會(huì)導(dǎo)致內(nèi)存泄露或潛在的崩潰問題,這里總結(jié)下c++ lambda捕獲this 導(dǎo)致多線程下類釋放后還在使用的錯(cuò)誤問題,感興趣的朋友一起看看吧2023-02-02使用C++實(shí)現(xiàn)Range序列生成器的示例代碼
在C++編程中,經(jīng)常需要迭代一系列數(shù)字或其他可迭代對(duì)象,本文將使用C++來實(shí)現(xiàn)一個(gè)簡單的Range封裝,文中的示例代碼講解詳細(xì),感興趣的可以了解下2023-11-11C++程序自動(dòng)重啟的實(shí)現(xiàn)代碼
自動(dòng)重啟原理很簡單,用一個(gè)進(jìn)程監(jiān)控另一個(gè)進(jìn)程,掛了就再啟動(dòng)一個(gè),細(xì)節(jié)也不算多,主要是正確判斷進(jìn)程狀態(tài)和啟動(dòng)方式,本文就給大家講講C++程序自動(dòng)重啟的實(shí)現(xiàn)方法,文中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考下2024-04-04