用C++11写一个线程池

1.用c++11实现线程池;

2.一个task队列,线程安全

3.一个线程池

4.线程池从task队列取任务

调用方法

//demo.cpp
#include <iostream>
#include <vector>
#include <chrono>

#include "threadpool.h"

int task1(int v)
{
    std::cout<<"task1 v="<<v<<std::endl;

    for(int i=0; i<10; i++)
    {
        std::cout<<" *task1["<<i<<"]*  ";
        //std::this_thread::sleep_for(std::chrono::seconds(1));
    }
    std::cout<<std::endl;
    return 1;
}

int task2(int v1,int v2)
{
    std::cout<<"task1 v1="<<v1<<" v2="<<v2<<std::endl;
    for(int i=0; i<10; i++)
    {
        std::cout<<" #task2["<<i<<"]#  ";
        //std::this_thread::sleep_for(std::chrono::seconds(1));
    }
    std::cout<<std::endl;
    return 2;
}

int task3(std::string str)
{
    std::cout<<"task 2 >>> "<<str<<std::endl;
    return 0;
}

int main()
{
    CThreadPool pool(2);
    pool.AddTask(task1, 1);
    pool.AddTask(task2, 2, 4);
    pool.AddTask(task3, std::string("hello ,thread pool. bye!"));

    getchar();
    pool.Release();
    std::cout << std::endl;
    
    return 0;
}

队列代码

//taskqueque.h
#ifndef TASK_QUEUE_H
#define TASK_QUEUE_H

#include <queue>
#include <mutex>
#include <condition_variable>

#define PRINT_FUNC std::cout<<__PRETTY_FUNCTION__<<"@@"<<__LINE__<<std::endl

class CTaskQueue{
public:
    CTaskQueue();
    ~CTaskQueue();

    template<class F, class... Args>
    auto PushTask(F&& f, Args&&... args)
    -> std::future<typename std::result_of<F(Args...)>::type>;
    auto GetTask();
    bool empty();
    void Wait();
    void Clear();
    int Size();
private:
    std::queue< std::function<void()> > m_queTasks;
    std::mutex m_muxTaskQue;
    std::condition_variable m_condition;
    bool m_bClearQueue;
};

 CTaskQueue::CTaskQueue():m_bClearQueue(false)
 {

 }

 CTaskQueue::~CTaskQueue()
 {
    PRINT_FUNC;
    Clear();
 }

 void CTaskQueue::Clear()
 {
    PRINT_FUNC;
    std::unique_lock<std::mutex> lock(m_muxTaskQue);
    
    while(!m_queTasks.empty())
    {
        m_queTasks.pop();
    }

    std::cout<<"Clear all task >>>>>>>"<<std::endl;
    
    m_bClearQueue = true;
    m_condition.notify_all();
 }

// 新task添加到queue
template<class F, class... Args>
auto CTaskQueue::PushTask(F&& f, Args&&... args) 
    -> std::future<typename std::result_of<F(Args...)>::type>
{
    std::cout<<__func__<<"@@"<<__LINE__<<std::endl;
    using return_type = typename std::result_of<F(Args...)>::type;

    std::cout<<__LINE__<<"@@PushTask"<<std::endl;

    auto task = std::make_shared< std::packaged_task<return_type()> >(
            std::bind(std::forward<F>(f), std::forward<Args>(args)...)
        );
        
    std::future<return_type> res = task->get_future();
    {
        std::unique_lock<std::mutex> lock(m_muxTaskQue);

        // don't allow enqueueing after stopping the pool
        if(m_bClearQueue)
            throw std::runtime_error("enqueue on stopped ThreadPool");

        m_queTasks.emplace([task](){ (*task)(); });
    }
    std::cout<<"will wake up <><><><>  task size="<<m_queTasks.size()<<std::endl;
    m_condition.notify_one();
    return res;
}

//get一个新task,同时task从队列中删除
auto CTaskQueue::GetTask()
{
    PRINT_FUNC;
    std::unique_lock<std::mutex> lock(m_muxTaskQue);
    auto task = m_queTasks.front();
    m_queTasks.pop();
    return task;
}

bool CTaskQueue::empty()
{
    PRINT_FUNC;
    std::unique_lock<std::mutex> lock(m_muxTaskQue);
    return m_queTasks.empty()?true:false;
}

//空闲的线程进入等待,当新的task进来,或者需要停止线程池时,执行线程
void CTaskQueue::Wait()
{
    PRINT_FUNC;
    std::unique_lock<std::mutex> lock(m_muxTaskQue);
    this->m_condition.wait(lock,[this]{ return this->m_bClearQueue || !this->m_queTasks.empty(); });
    std::cout<<"task active!!!  task size="<<m_queTasks.size()<<std::endl;
}

int CTaskQueue::Size()
{
    PRINT_FUNC;
    std::unique_lock<std::mutex> lock(m_muxTaskQue);
    return m_queTasks.size();
}
#endif

线程池代码

//threadpool.h
#ifndef THREAD_POOL_H
#define THREAD_POOL_H

#include <vector>
#include <memory>
#include <thread>
#include <future>
#include <functional>
#include <stdexcept>
#include "taskqueue.h"

class CThreadPool {
public:
    CThreadPool(size_t);
    template<class F, class... Args>
    auto AddTask(F&& f, Args&&... args) 
        -> std::future<typename std::result_of<F(Args...)>::type>;
    ~CThreadPool();
    void OnWorkThread();
    void Release();
private:
    // need to keep track of threads so we can join them
    std::vector< std::thread > workers;
    bool m_bStop;
    CTaskQueue m_taskQueque;
};

//工作线程
void CThreadPool::OnWorkThread()
{
    PRINT_FUNC;
    while(true)
    {
        std::function<void()> task;
        //等待新task,或者结束的消息
        m_taskQueque.Wait();

        if(m_bStop && m_taskQueque.empty())
        {
            return;
        }
        std::cout<<__LINE__<<" ## thread id="<<std::this_thread::get_id()<<std::endl;
        std::cout<<__LINE__<<" ## task queue size="<<m_taskQueque.Size()<<std::endl;

        if(m_taskQueque.empty())
        {
            std::cout<<__LINE__<<" ## no task"<<std::endl;
            continue;
        }
        //从task queue里面,取出一个task
        task = std::move(m_taskQueque.GetTask());
        //std::this_thread::sleep_for (std::chrono::seconds(2));

        //执行task
        std::cout<<"task begin ......"<<std::endl;
        task();
        std::cout<<"task finish ......"<<std::endl;
    }
}
 
//预先创建多个工作线程
inline CThreadPool::CThreadPool(size_t threads)
    :   m_bStop(false)
{
    PRINT_FUNC;
    for(size_t i = 0;i<threads;++i)
        workers.emplace_back([this]{
            OnWorkThread();
        });
}

// add new work item to the pool
template<class F, class... Args>
auto CThreadPool::AddTask(F&& f, Args&&... args) 
    -> std::future<typename std::result_of<F(Args...)>::type>
{
    std::cout<<__func__<<"@@"<<__LINE__<<std::endl;
    return m_taskQueque.PushTask(f, args... );
}

inline CThreadPool::~CThreadPool()
{
    PRINT_FUNC;
    Release();
}

void CThreadPool::Release()
{
    PRINT_FUNC;
    if(m_bStop)
    {
        return;
    }

    m_bStop = true;
    m_taskQueque.Clear();
    for(std::thread &worker: workers)
    {
        worker.join();
        std::cout<<"join..."<<std::endl;
    }
}

#endif

CMakeLists.txt

CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
PROJECT(demo)

add_compile_options(-std=c++11)

MESSAGE(DEBUG "CMAKE_CURRENT_LIST_DIR:" ${CMAKE_CURRENT_LIST_DIR})

INCLUDE_DIRECTORIES(
    ${CMAKE_CURRENT_LIST_DIR}/
)

FILE(GLOB SOURCES
    ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp
)

ADD_EXECUTABLE(
    ${PROJECT_NAME} 
    ${SOURCES}
)

TARGET_LINK_LIBRARIES(
    ${PROJECT_NAME}
    pthread
)

执行输出

[root@VM-20-16-centos demo]# ./demo 
CThreadPool::CThreadPool(size_t)@@65
AddTask@@77
PushTask@@62
65@@PushTask
will wake up <><><><>  task size=1
void CThreadPool::OnWorkThread()@@31
void CTaskQueue::Wait()@@104
task active!!!  task size=1
42 ## thread id=140702146483968
int CTaskQueue::Size()@@112
43 ## task queue size=1
bool CTaskQueue::empty()@@97
auto CTaskQueue::GetTask()@@88
AddTask@@77
PushTask@@62
65@@PushTask
void CThreadPool::OnWorkThread()@@31
void CTaskQueue::Wait()@@104
task active!!!  task size=1
42 ## thread id=140702138091264
int CTaskQueue::Size()@@112
43 ## task queue size=1
bool CTaskQueue::empty()@@task begin ......
97
auto CTaskQueue::GetTask()@@88
task1 v=1
 *task1[0]*   *task1[1]*   *task1[2]*   *task1[3]*   *task1[4]*   *task1[5]*   *task1[6]*   *task1[7]*   *task1[8]*   *task1[9]*  
task finish ......
task begin ......
void CTaskQueue::Wait()@@task1 v1=104
will wake up <><><><>  task size=1
AddTask@@77
PushTask@@62
65@@PushTask
will wake up <><><><>  task size=1
task active!!!  task size=1
42 ## thread id=140702146483968
int CTaskQueue::Size()@@112
43 ## task queue size=1
bool CTaskQueue::empty()@@97
auto CTaskQueue::GetTask()@@88
task begin ......
task 2 >>> hello ,thread pool. bye!
task finish ......
void CTaskQueue::Wait()@@104
2 v2=4
 #task2[0]#   #task2[1]#   #task2[2]#   #task2[3]#   #task2[4]#   #task2[5]#   #task2[6]#   #task2[7]#   #task2[8]#   #task2[9]#  
task finish ......
void CTaskQueue::Wait()@@104

void CThreadPool::Release()@@90
void CTaskQueue::Clear()@@43
Clear all task >>>>>>>
task active!!!  task size=0
bool CTaskQueue::empty()@@97
task active!!!  task size=0
bool CTaskQueue::empty()@@97
join...
join...

CThreadPool::~CThreadPool()@@84
void CThreadPool::Release()@@90
CTaskQueue::~CTaskQueue()@@37
void CTaskQueue::Clear()@@43
Clear all task >>>>>>>
[root@VM-20-16-centos demo]# 

完!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值