如何实现C++中std::thread的线程暂停(挂起)功能?
- 内容介绍
- 文章标签
- 相关推荐
本文共计873个文字,预计阅读时间需要4分钟。
目录
一、封装Thread类
二、测试代码
一、封装Thread类
为了更好地管理线程,我们可以封装一个Thread类,基于C++11中与平台无关的线程类std::thread,并为其提供start()、stop()、pause()、resume()线程控制方法。二、测试代码
cpp#include #include #includeclass ThreadWrapper {public: ThreadWrapper() : thread(nullptr) {} ~ThreadWrapper() { stop(); }
void start() { if (thread==nullptr) { thread=new std::thread(&ThreadWrapper::run, this); } }
void stop() { if (thread !=nullptr) { pause(); thread->join(); delete thread; thread=nullptr; } }
void pause() { if (thread !=nullptr) { std::unique_lock lock(mutex); running=false; } }
void resume() { if (thread !=nullptr) { std::unique_lock lock(mutex); running=true; cond.notify_one(); } }
private: void run() { while (true) { std::unique_lock lock(mutex); cond.wait(lock, [this] { return running; }); // Perform the thread's work here std::cout << Thread is running... < std::thread* thread; std::mutex mutex; std::condition_variable cond; bool running=true;}; int main() { ThreadWrapper tw; tw.start(); std::this_thread::sleep_for(std::chrono::seconds(5)); tw.pause(); std::cout << Thread paused... < std::this_thread::sleep_for(std::chrono::seconds(2)); tw.resume(); std::cout << Thread resumed... < std::this_thread::sleep_for(std::chrono::seconds(5)); tw.stop(); std::cout << Thread stopped... < return 0;} 我们基于C++11中与平台无关的线程类std::thread,封装Thread类,并提供start()、stop()、pause()、resume()线程控制方法。 为了让线程在暂停期间,处于休眠,不消耗CPU,我们使用C++11提供的锁和条件变量来实现。 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线程实现暂停(挂起)功能的文章就介绍到这了,更多相关C++11 std::thread线程暂停内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!目录
一、封装Thread类
二、测试代码
本文共计873个文字,预计阅读时间需要4分钟。
目录
一、封装Thread类
二、测试代码
一、封装Thread类
为了更好地管理线程,我们可以封装一个Thread类,基于C++11中与平台无关的线程类std::thread,并为其提供start()、stop()、pause()、resume()线程控制方法。二、测试代码
cpp#include #include #includeclass ThreadWrapper {public: ThreadWrapper() : thread(nullptr) {} ~ThreadWrapper() { stop(); }
void start() { if (thread==nullptr) { thread=new std::thread(&ThreadWrapper::run, this); } }
void stop() { if (thread !=nullptr) { pause(); thread->join(); delete thread; thread=nullptr; } }
void pause() { if (thread !=nullptr) { std::unique_lock lock(mutex); running=false; } }
void resume() { if (thread !=nullptr) { std::unique_lock lock(mutex); running=true; cond.notify_one(); } }
private: void run() { while (true) { std::unique_lock lock(mutex); cond.wait(lock, [this] { return running; }); // Perform the thread's work here std::cout << Thread is running... < std::thread* thread; std::mutex mutex; std::condition_variable cond; bool running=true;}; int main() { ThreadWrapper tw; tw.start(); std::this_thread::sleep_for(std::chrono::seconds(5)); tw.pause(); std::cout << Thread paused... < std::this_thread::sleep_for(std::chrono::seconds(2)); tw.resume(); std::cout << Thread resumed... < std::this_thread::sleep_for(std::chrono::seconds(5)); tw.stop(); std::cout << Thread stopped... < return 0;} 我们基于C++11中与平台无关的线程类std::thread,封装Thread类,并提供start()、stop()、pause()、resume()线程控制方法。 为了让线程在暂停期间,处于休眠,不消耗CPU,我们使用C++11提供的锁和条件变量来实现。 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线程实现暂停(挂起)功能的文章就介绍到这了,更多相关C++11 std::thread线程暂停内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!目录
一、封装Thread类
二、测试代码

