默認的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;
}