当前文章: 首页 >> 编程开发 >> Qt >> C++ ThreadPool
聚划算
«   2019年11月   »
123
45678910
11121314151617
18192021222324
252627282930
云盾证书
网站分类
    海外云服务器CPS_轻量拓展全民云计算企业云服务器云盾证书
标签列表
最近发表

C++ ThreadPool

广告位250x250
内容

记得以前学习boost的时候接触到boost::threadpool,真是高大尚,支持各种常用的线程管理。

后来发现使用boost显得有些笨重,因为boost中太多模板化的东西,引用的头文件很多,编译起来很慢,像我要的这种小功能一般用不上。

搜索了一下百度,还真发现有使用C++11就能实现threadpool功能,而且支持可变长参数。

参考:基于C++11的线程池(threadpool),简洁且可以带任意多的参数

 

后来发现并不能满足我的需求,于是参考上面链接中的文章,自己使用一天时间重头写了一个,已经保存在github,感觉还可以,欢迎测试反馈。

(使用)源码:

main.cpp

#include <iostream>#include "threadpool.h"using namespace std;void test1()
{    cout<<"test1: tid="<<this_thread::get_id()<<endl;
}int test2( int a, int b )
{    cout<<"test2: tid="<<this_thread::get_id()<<", a="<<a<<", b="<<b<<endl;    //
    return a+b;
}void test3( const size_t milliseconds )
{    cout<<"test3: tid="<<this_thread::get_id()<<" sleep begin."<<endl;
    jues::ThreadPool::sleep(milliseconds);    cout<<"test3: tid="<<this_thread::get_id()<<" sleep end."<<endl;
}// 按先入先出来管理线程池void thread_fifo_pool()
{
    jues::ThreadPool fifo_pool( jues::ThreadPool::TYPE_FIFO,1 );    //
    fifo_pool.start();
    fifo_pool.commit(test1);
    fifo_pool.commit(test2,1,2);
    fifo_pool.commit(test3,1000);    //
    fifo_pool.wait();
}// 按先入后出来管理线程池void thread_filo_pool()
{
    jues::ThreadPool filo_pool( jues::ThreadPool::TYPE_FILO,1 );    //
    filo_pool.start();
    filo_pool.commit(test1);
    filo_pool.commit(test2,1,2);
    filo_pool.commit(test3,1000);    //
    filo_pool.wait();
}// 按优先级来管理线程池void thread_level_pool()
{
    jues::ThreadPool level_pool( jues::ThreadPool::TYPE_LEVEL,1 );    //
    level_pool.start();
    level_pool.commit(-1,test1);
    level_pool.commit(10,test2,1,2);
    level_pool.commit(100,test3,1000);    //
    level_pool.wait();
}// 运行中动态调整线程池数量void thread_resize_pool()
{
    jues::ThreadPool fifo_pool( jues::ThreadPool::TYPE_FIFO,2 );    //
    fifo_pool.start();    for ( int i =0;10 >i;i++ )
    {
        fifo_pool.commit(test3,1000);
    }
    fifo_pool.sleep(1000);
    fifo_pool.resize(1);    //
    fifo_pool.wait();
}// demo类class demo
{public:
    demo() {}    void run( const size_t milliseconds )
    {        cout<<"demo::run: tid="<<this_thread::get_id()<<" sleep begin."<<endl;
        jues::ThreadPool::sleep(milliseconds);        cout<<"demo::run: tid="<<this_thread::get_id()<<" sleep end."<<endl;
    }
};// 使用类中的函数void thread_class_function_pool()
{
    jues::ThreadPool level_pool( jues::ThreadPool::TYPE_LEVEL,1 );
    demo d;    //
    level_pool.start();
    level_pool.commit(10,std::bind(&demo::run,&d,1000));    //
    level_pool.wait();
}// 主函数int main()
{    cout<<"main: tid="<<this_thread::get_id()<<", thread_fifo_pool()----------------"<<endl;
    thread_fifo_pool();    cout<<"main: tid="<<this_thread::get_id()<<", thread_filo_pool()----------------"<<endl;
    thread_filo_pool();    cout<<"main: tid="<<this_thread::get_id()<<", thread_level_pool()---------------"<<endl;
    thread_level_pool();    cout<<"main: tid="<<this_thread::get_id()<<", thread_resize_pool()---------------"<<endl;
    thread_resize_pool();    cout<<"main: tid="<<this_thread::get_id()<<", thread_class_function_pool()---------------"<<endl;
    thread_class_function_pool();    //
    cin.get();    return 0;
}


测试结果:

main: tid=1, thread_fifo_pool()----------------
test1: tid=2
test2: tid=2, a=1, b=2
test3: tid=2 sleep begin.
test3: tid=2 sleep end.
main: tid=1, thread_filo_pool()----------------
test1: tid=3
test3: tid=3 sleep begin.
test3: tid=3 sleep end.
test2: tid=3, a=1, b=2
main: tid=1, thread_level_pool()---------------
test1: tid=4
test3: tid=4 sleep begin.
test3: tid=4 sleep end.
test2: tid=4, a=1, b=2
main: tid=1, thread_resize_pool()---------------
test3: tid=6 sleep begin.
test3: tid=5 sleep begin.
test3: tid=test3: tid=6 sleep end.
5 sleep end.
test3: tid=5 sleep begin.
test3: tid=5 sleep end.
test3: tid=5 sleep begin.
test3: tid=5 sleep end.
test3: tid=5 sleep begin.
test3: tid=5 sleep end.
test3: tid=5 sleep begin.
test3: tid=5 sleep end.
test3: tid=5 sleep begin.
test3: tid=5 sleep end.
test3: tid=5 sleep begin.
test3: tid=5 sleep end.
test3: tid=5 sleep begin.
test3: tid=5 sleep end.
test3: tid=5 sleep begin.
test3: tid=5 sleep end.
main: tid=1, thread_class_function_pool()---------------
demo::run: tid=7 sleep begin.
demo::run: tid=7 sleep end.


原文请移步: http://note.jues.org.cn/node/357

声明:本站原创文章,转载、复制、使用等请注明出处。
标题C++ ThreadPool
作者jues
地址: http://blog.jues.org.cn/post/c-threadpool.html

发表评论:

Powered By Z-BlogPHP 1.5.2 Zero Powered By Z-BlogPHP © 2018 jues博客 Copyright Your WebSite.Some Rights Reserved.联系站长 Themes by jues