io_service::run在没有任务运行时会退出,下次post任务时需要重新执行run函数才能运行任务,但在实际项目中我们并不想每次多执行post,此时可以使用io_service::run来解决此问题。
注意:此功能需要在线程下才起作用,执行在主线程是无法起作用的。
#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include <boost/scoped_ptr.hpp>
#include <iostream>
using namespace boost::asio;
io_service service;
io_service::work g_work(service);
void func(int i) {
std::cout << "func was called, i= " << i << std::endl;
}
void start() {
service.run();
}
void stop() {
service.stop();
}
int main(int argc, char* argv[]) {
service.post(boost::bind(func, 10));
boost::thread th(start);
//start(); // 注意,直接在main函数启动无法实现work功能
service.post(boost::bind(func, 11));
// 如果没有io_service::work,以下代码不会运行。
boost::this_thread::sleep(boost::posix_time::millisec(1000));
for (int i = 0; i < 10; ++i)
service.post(boost::bind(func, i));
boost::this_thread::sleep(boost::posix_time::millisec(1000));
for (int i = 100; i < 110; ++i)
service.post(boost::bind(func, i));
// wait for all threads to be created
boost::this_thread::sleep(boost::posix_time::millisec(500));
//th.join();
system("pause");
return 0;
}