C++11中std::thread线程实现暂停(挂起)功能

 更新时间:2023年04月23日 09:57:32   作者:百里杨  
本文主要介绍了C++11中std::thread线程实现暂停(挂起)功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

一、封装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线程实现暂停(挂起)功能的文章就介绍到这了,更多相关C++11 std::thread线程暂停内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 深入浅析 C++ 调用 Python 模块

    深入浅析 C++ 调用 Python 模块

    Python 提供了 C++ 库,使得开发者能很方便地从 C++ 程序中调用 Python 模块。接下来通过本文给大家介绍 C++ 调用 Python 模块的相关知识,需要的朋友参考下吧
    2016-03-03
  • 带你深度走入C语言取整以及4种函数

    带你深度走入C语言取整以及4种函数

    大家都知道取整这回事,但是对于取整只有单一的认识,下面这篇文章主要给大家介绍了关于C语言取整以及4种函数的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-08-08
  • 深入串的模式匹配算法(普通算法和KMP算法)的详解

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

    本篇文章是对串的模式匹配算法(普通算法和KMP算法)的应用进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • 解析C++中的5个存储类的作用

    解析C++中的5个存储类的作用

    这篇文章主要介绍了C++中的5个存储类的作用,存储类是管理对象的生存期、链接和内存位置的类型说明符,需要的朋友可以参考下
    2016-05-05
  • C++类的分离式写法介绍示例

    C++类的分离式写法介绍示例

    今天小编就为大家分享一篇关于C++类的分离式写法介绍示例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-12-12
  • 一文详解C++中隐含的this指针

    一文详解C++中隐含的this指针

    这篇文章主要带大家详细了解一下C++中隐含的this指针,文中通过代码示例和图文介绍的非常详细,对大家的学习或工作有一定的帮助,需要的朋友可以参考下
    2024-01-01
  • C++实现Matlab的zp2tf函数的示例代码

    C++实现Matlab的zp2tf函数的示例代码

    matlab 的 zp2tf 函数的作用是将极点形式的 H(s) 函数的分母展开,本文主要为大家介绍了C++实现Matlab的zp2tf函数示例代码,需要的可以参考一下
    2023-04-04
  • C++线程优先级SetThreadPriority的使用实例

    C++线程优先级SetThreadPriority的使用实例

    这篇文章主要介绍了C++线程优先级SetThreadPriority的使用实例,较为详细的讲述了C++线程及其优先级的用法,需要的朋友可以参考下
    2014-10-10
  • C语言中数据如何存储进内存揭秘

    C语言中数据如何存储进内存揭秘

    使用编程语言进行编程时,需要用到各种变量来存储各种信息。变量保留的是它所存储的值的内存位置。这意味着,当您创建一个变量时,就会在内存中保留一些空间。您可能需要存储各种数据类型的信息,操作系统会根据变量的数据类型,来分配内存和决定在保留内存中存储什么
    2022-08-08
  • Dev-C++无法使用bits/stdc++.h问题及解决

    Dev-C++无法使用bits/stdc++.h问题及解决

    这篇文章主要介绍了Dev-C++无法使用bits/stdc++.h问题及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-08-08

最新评论