C++ Boost ThreadPool 线程池


默认的boost针对线程的支持中不存在线程池功能,我们可以下载一个boost::threadpool来让其支持线程池.

项目地址: http://threadpool.sourceforge.net/

首先来看一下,如何实现无参数和有参数的调用,同上这里就不在解释了.

#include <iostream>
#include <string>
#include <boost/bind.hpp>
#include <boost/threadpool.hpp>

void first_task()
{
    std::cout << "hello lyshark" << std::endl;
}

void last_task(int uuid, std::string uname)
{
    std::cout << "UUID: " << uuid << " Uname: " << uname << std::endl;
}

int main(int argc,char *argv[])
{
    // 初始化为4个线程
    boost::threadpool::pool pool(4);

    // 无参数调用
    pool.schedule(&first_task);
    pool.wait();

    // 有参数调用
    pool.schedule(boost::bind(last_task, 1001, "lyshark"));
    pool.wait();
    
    std::system("pause");
    return 0;
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM