1 封装std::thread,增加子线程启动暂停、唤起、停止功能

C++标准库的std::thread已经为C++增加了很好的跨平台多线程编程体验,程序员只需要使用标准库std::thread就可以写出很好的多线程并发程序。但是在std::thread中对线程的控制需要自主控制。

我们经常会在子线程函数中写一个while循环去处理队列任务,当任务全部处理完成之后需要挂起线程,减少CPU负载,所以对应这个需求对std::thread进行了封装,实现一个扩展的std::thread基类,在基类中提供开启线程、暂停线程、唤起线程、停止线程的函数,并提供一个纯虚函数接口CustomRun,用于在子类中扩展自己的线程处理函数。

1.1 封装代码

代码如下:

ThreadExtension.h

#ifndef THREAD_EXTENSION_H
#define THREAD_EXTENSION_H

#include <thread>
#include <mutex>
#include <condition_variable>
#include <atomic>

class ThreadExtension
{
    enum class ThreadState
    {
        Stoped = 0,
        Running = 1,
        Paused = 2
    };
public:
    ThreadExtension();
    virtual~ThreadExtension();

public:
    void StartThread();
    void PauseThread();
    void ResumeThread();
    void StopThread();

    int GetThreadState() const;

private:
    void Run();

protected:
    virtual void CustomRun() {};

private:
    ThreadState m_Thread_State;
    std::shared_ptr<std::thread> m_pThread;
    std::mutex m_Mutex;
    std::condition_variable m_Condition_Variable;
    std::atomic<bool> m_Thread_Pause_Flag;
    std::atomic<bool> m_Thread_Stop_Flag;

};

#endif // !THREAD_EXTENSION_H

ThreadExtension.cpp

#include "ThreadExtension.h"

ThreadExtension::ThreadExtension()
    :m_pThread(nullptr),
    m_Thread_Pause_Flag(false),
    m_Thread_Stop_Flag(false),
    m_Thread_State(ThreadState::Stoped)
{

}

ThreadExtension::~ThreadExtension()
{
    StopThread();
}

void ThreadExtension::StartThread()
{
    if (m_pThread == nullptr)
    {
        m_pThread = std::make_shared<std::thread>(&ThreadExtension::Run, this);

        if (m_pThread != nullptr)
        {
            m_Thread_Pause_Flag = false;
            m_Thread_Stop_Flag = false;
            m_Thread_State = ThreadState::Running;
        }
    }
}

void ThreadExtension::PauseThread()
{
    if (m_pThread != nullptr)
    {
        if (m_Thread_State == ThreadState::Running)
        {
            m_Thread_Pause_Flag = true;
            m_Thread_State = ThreadState::Paused;
        }
    }
}

void ThreadExtension::ResumeThread()
{
    if (m_pThread != nullptr)
    {
        if (m_Thread_State == ThreadState::Paused)
        {
            m_Thread_Pause_Flag = false;
            m_Condition_Variable.notify_all();
            m_Thread_State = ThreadState::Running;
        }
    }
}

void ThreadExtension::StopThread()
{
    if (m_pThread != nullptr)
    {
        m_Thread_Stop_Flag = true;
        m_Thread_Pause_Flag = false;    

        m_Condition_Variable.notify_all();

        if (m_pThread->joinable())
        {
            m_pThread->join();
        }

        // 释放
        m_pThread.reset();

        if (m_pThread == nullptr)
        {
            m_Thread_State = ThreadState::Stoped;
        }
    }
}

int ThreadExtension::GetThreadState() const
{
    return (int)m_Thread_State;
}

void ThreadExtension::Run()
{
    while (!m_Thread_Stop_Flag)
    {
        // 指定自定义线程操作
        try 
        {
            CustomRun();
        }
        catch (std::exception& e)
        {
            break;
        }

        // 暂停
        if (m_Thread_Pause_Flag)
        {
            std::unique_lock<std::mutex> thread_locker(m_Mutex);
            if (m_Thread_Pause_Flag)
            {
                // 等待互斥锁
                m_Condition_Variable.wait(thread_locker);
            }

            thread_locker.unlock();

        }
    }

    m_Thread_Pause_Flag = false;
    m_Thread_Stop_Flag = false;
}


在上述ThreadExtension类中,在StartThread函数中创建了一个以成员函数Run为线程函数的子线程,在Run函数中通过原子bool变量m_Thread_Stop_Flag控制线程函数开始或者停止,通过原子变量m_Thread_Pause_Flag控制线程函数暂停或者唤醒,在m_Thread_Pause_Flag为真时,使用std::condition_variable和std::mutex等待线程函数被重新唤醒继续执行。

1.2 使用示例

#include <iostream>

#include "ThreadExtension.h"

class TestThread :public ThreadExtension
{
public:
    TestThread()
    {

    }

    virtual ~TestThread()
    {

    }

    void CustomRun() override
    {
        std::cout << "CustomRun" << std::endl;
    }
};

int main()
{
    TestThread thread;
    std::cout << "Thread State:" << thread.GetThreadState() << std::endl;

    thread.StartThread();
    std::cout << "Thread State:" << thread.GetThreadState() << std::endl;
    std::this_thread::sleep_for(std::chrono::milliseconds(10));

    thread.PauseThread();
    std::cout << "Thread State:" << thread.GetThreadState() << std::endl;

    std::this_thread::sleep_for(std::chrono::milliseconds(11100));


    thread.ResumeThread();
    std::cout << "Thread State:" << thread.GetThreadState() << std::endl;

    std::this_thread::sleep_for(std::chrono::milliseconds(10));

    thread.StopThread();
    std::cout << "Thread State:" << thread.GetThreadState() << std::endl;

    getchar();

    return 0;
}

运行结果:

C++11 – 封装std::thread,增加子线程启动、暂停、唤起、停止功能-StubbornHuang Blog