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;
}
posted @ 2021-10-19 16:31  lyshark  阅读(3354)  评论(0)    收藏  举报