最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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++ 調(diào)用 Python 模塊

    深入淺析 C++ 調(diào)用 Python 模塊

    Python 提供了 C++ 庫,使得開發(fā)者能很方便地從 C++ 程序中調(diào)用 Python 模塊。接下來通過本文給大家介紹 C++ 調(diào)用 Python 模塊的相關(guān)知識(shí),需要的朋友參考下吧
    2016-03-03
  • 帶你深度走入C語言取整以及4種函數(shù)

    帶你深度走入C語言取整以及4種函數(shù)

    大家都知道取整這回事,但是對(duì)于取整只有單一的認(rèn)識(shí),下面這篇文章主要給大家介紹了關(guān)于C語言取整以及4種函數(shù)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-08-08
  • 深入串的模式匹配算法(普通算法和KMP算法)的詳解

    深入串的模式匹配算法(普通算法和KMP算法)的詳解

    本篇文章是對(duì)串的模式匹配算法(普通算法和KMP算法)的應(yīng)用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • 解析C++中的5個(gè)存儲(chǔ)類的作用

    解析C++中的5個(gè)存儲(chǔ)類的作用

    這篇文章主要介紹了C++中的5個(gè)存儲(chǔ)類的作用,存儲(chǔ)類是管理對(duì)象的生存期、鏈接和內(nèi)存位置的類型說明符,需要的朋友可以參考下
    2016-05-05
  • C++類的分離式寫法介紹示例

    C++類的分離式寫法介紹示例

    今天小編就為大家分享一篇關(guān)于C++類的分離式寫法介紹示例,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • 一文詳解C++中隱含的this指針

    一文詳解C++中隱含的this指針

    這篇文章主要帶大家詳細(xì)了解一下C++中隱含的this指針,文中通過代碼示例和圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-01-01
  • C++實(shí)現(xiàn)Matlab的zp2tf函數(shù)的示例代碼

    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)先級(jí)SetThreadPriority的使用實(shí)例

    C++線程優(yōu)先級(jí)SetThreadPriority的使用實(shí)例

    這篇文章主要介紹了C++線程優(yōu)先級(jí)SetThreadPriority的使用實(shí)例,較為詳細(xì)的講述了C++線程及其優(yōu)先級(jí)的用法,需要的朋友可以參考下
    2014-10-10
  • C語言中數(shù)據(jù)如何存儲(chǔ)進(jìn)內(nèi)存揭秘

    C語言中數(shù)據(jù)如何存儲(chǔ)進(jìn)內(nèi)存揭秘

    使用編程語言進(jìn)行編程時(shí),需要用到各種變量來存儲(chǔ)各種信息。變量保留的是它所存儲(chǔ)的值的內(nèi)存位置。這意味著,當(dāng)您創(chuàng)建一個(gè)變量時(shí),就會(huì)在內(nèi)存中保留一些空間。您可能需要存儲(chǔ)各種數(shù)據(jù)類型的信息,操作系統(tǒng)會(huì)根據(jù)變量的數(shù)據(jù)類型,來分配內(nèi)存和決定在保留內(nèi)存中存儲(chǔ)什么
    2022-08-08
  • Dev-C++無法使用bits/stdc++.h問題及解決

    Dev-C++無法使用bits/stdc++.h問題及解決

    這篇文章主要介紹了Dev-C++無法使用bits/stdc++.h問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08

最新評(píng)論

固镇县| 长治市| 鸡泽县| 宝清县| 平原县| 吴旗县| 丽江市| 佛坪县| 德阳市| 鱼台县| 观塘区| 综艺| 罗平县| 仪陇县| 上蔡县| 龙江县| 额尔古纳市| 杭锦后旗| 乌鲁木齐市| 延安市| 新乐市| 黔江区| 威远县| 汉源县| 景泰县| 洪雅县| 东乡县| 蒲城县| 普宁市| 中阳县| 寿宁县| 武乡县| 莱阳市| 玉龙| 潮安县| 兰溪市| 清新县| 永宁县| 金堂县| 江孜县| 永嘉县|