欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++11中std::thread線程實(shí)現(xiàn)暫停(掛起)功能

 更新時(shí)間:2023年04月23日 09:57:32   作者:百里楊  
本文主要介紹了C++11中std::thread線程實(shí)現(xiàn)暫停(掛起)功能,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

一、封裝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ù)組示例詳解

    這篇文章主要為大家介紹了C語言動(dòng)態(tài)內(nèi)存管理malloc柔性數(shù)組示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • C語言數(shù)據(jù)類型與sizeof關(guān)鍵字

    C語言數(shù)據(jù)類型與sizeof關(guān)鍵字

    這篇文章主要介紹了C語言數(shù)據(jù)類型與sizeof關(guān)鍵字,C語言的數(shù)據(jù)類型包括基本類型、構(gòu)造類型、指針類型以及空類型,下文更多相關(guān)內(nèi)容需要的小伙伴可以參考一下
    2022-04-04
  • C++頭文件和cpp文件的原理分析

    C++頭文件和cpp文件的原理分析

    這篇文章主要介紹了C++頭文件和cpp文件的原理分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • c++ lambda捕獲this 導(dǎo)致多線程下類釋放后還在使用的錯(cuò)誤問題

    c++ 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
  • 一起來學(xué)習(xí)C++中類的this指針以使用

    一起來學(xué)習(xí)C++中類的this指針以使用

    這篇文章主要為大家詳細(xì)介紹了C++中類的this指針以使用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • OpenCV實(shí)現(xiàn)圖像拼接案例

    OpenCV實(shí)現(xiàn)圖像拼接案例

    這篇文章主要介紹了OpenCV實(shí)現(xiàn)圖像拼接案例,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下
    2022-08-08
  • 使用C++實(shí)現(xiàn)Range序列生成器的示例代碼

    使用C++實(shí)現(xiàn)Range序列生成器的示例代碼

    在C++編程中,經(jīng)常需要迭代一系列數(shù)字或其他可迭代對(duì)象,本文將使用C++來實(shí)現(xiàn)一個(gè)簡單的Range封裝,文中的示例代碼講解詳細(xì),感興趣的可以了解下
    2023-11-11
  • C/C++實(shí)現(xiàn)三路快速排序算法原理

    C/C++實(shí)現(xiàn)三路快速排序算法原理

    這篇文章主要為大家詳細(xì)介紹了C/C++實(shí)現(xiàn)三路快速排序算法原理,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • C++程序自動(dòng)重啟的實(shí)現(xiàn)代碼

    C++程序自動(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
  • C++字符串的截取問題

    C++字符串的截取問題

    這篇文章主要介紹了C++字符串的截取問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08

最新評(píng)論