您好,登錄后才能下訂單哦!
本篇內容主要講解“C++11中std::thread線程怎么實現暫停功能”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“C++11中std::thread線程怎么實現暫停功能”吧!
我們基于C++11中與平臺無關的線程類std::thread,封裝Thread類,并提供start()、stop()、pause()、resume()線程控制方法。
為了讓線程在暫停期間,處于休眠,不消耗CPU,我們使用C++11提供的鎖和條件變量來實現。
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, ///<停止狀態,包括從未啟動過和啟動后被停止 Running, ///<運行狀態 Paused ///<暫停狀態 }; 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(); }
運行結果:
到此,相信大家對“C++11中std::thread線程怎么實現暫停功能”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。