这个过程非常简单。首先创建一个asio :: io_service和一个thread_group。使用链接到io_service的线程填充thread_group。使用boost :: bind函数将任务分配给线程。
要停止线程(通常在退出程序时),只需停止io_service并加入所有线程。
你应该只需要这些标题:
#include
#include
#include
这是一个例子:
/*
* Create an asio::io_service and a thread_group (through pool in essence)
*/
boost::asio::io_service ioService;
boost::thread_group threadpool;
/*
* This will start the ioService processing loop. All tasks
* assigned with ioService.post() will start executing.
*/
boost::asio::io_service::work work(ioService);
/*
* This will add 2 threads to the thread pool. (You could just put it in a for loop)
*/
threadpool.create_thread(
boost::bind(&boost::asio::io_service::run, &ioService)
);
threadpool.create_thread(
boost::b