C++11中std::thread線程實(shí)現(xiàn)暫停(掛起)功能
一、封裝Thread類
我們基于C++11中與平臺無關(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),包括從未啟動過和啟動后被停止
? ? ? ? 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)識
? ? std::atomic_bool _stopFlag; ? ///<停止標(biāo)識
? ? State _state;
};
#endif // THREAD_HThread.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++實(shí)現(xiàn)Matlab的zp2tf函數(shù)的示例代碼
matlab?的?zp2tf?函數(shù)的作用是將極點(diǎn)形式的?H(s)?函數(shù)的分母展開,本文主要為大家介紹了C++實(shí)現(xiàn)Matlab的zp2tf函數(shù)示例代碼,需要的可以參考一下2023-04-04
C++線程優(yōu)先級SetThreadPriority的使用實(shí)例
這篇文章主要介紹了C++線程優(yōu)先級SetThreadPriority的使用實(shí)例,較為詳細(xì)的講述了C++線程及其優(yōu)先級的用法,需要的朋友可以參考下2014-10-10
C語言中數(shù)據(jù)如何存儲進(jìn)內(nèi)存揭秘
使用編程語言進(jìn)行編程時,需要用到各種變量來存儲各種信息。變量保留的是它所存儲的值的內(nèi)存位置。這意味著,當(dāng)您創(chuàng)建一個變量時,就會在內(nèi)存中保留一些空間。您可能需要存儲各種數(shù)據(jù)類型的信息,操作系統(tǒng)會根據(jù)變量的數(shù)據(jù)類型,來分配內(nèi)存和決定在保留內(nèi)存中存儲什么2022-08-08

