C++ 为什么我们需要使用 boost::asio::io_service::work?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17156541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why do we need to use boost::asio::io_service::work?
提问by Alex
There is an example of using boost::asio.
有一个使用 boost::asio 的例子。
- Why does this example use the boost::asio::io_service::work ?
- And why is
srv.run ();
not called to perform tasks in the threads?
- 为什么这个例子使用 boost::asio::io_service::work ?
- 为什么
srv.run ();
不调用在线程中执行任务?
int main()
{
boost::asio::io_service srv;
boost::asio::io_service::work work(srv);
boost::thread_group thr_grp;
thr_grp.create_thread(boost::bind(&boost::asio::io_service::run, &srv));
thr_grp.create_thread(boost::bind(&boost::asio::io_service::run, &srv));
srv.post(boost::bind(f1, 123));
srv.post(boost::bind(f1, 321));
//sync
srv.post(boost::bind(f2, 456));
srv.post(boost::bind(f2, 654));
//sync
srv.stop();
thr_grp.join();
}
Update:What is the difference between the poll and run, when io_service is used without io_service::work?
更新:在没有 io_service::work 的情况下使用 io_service 时,poll 和 run 之间有什么区别?
int main()
{
boost::asio::io_service srv;
//boost::asio::io_service::work work(srv);
std::vector<boost::thread> thr_grp;
srv.post(boost::bind(f1, 123));
srv.post(boost::bind(f1, 321));
//sync
srv.post(boost::bind(f2, 456));
srv.post(boost::bind(f2, 654));
//sync
// What is the difference between the poll and run, when io_service without work?
thr_grp.emplace_back(boost::bind(&boost::asio::io_service::poll, &srv));// poll or run?
thr_grp.emplace_back(boost::bind(&boost::asio::io_service::run, &srv));// poll or run?
srv.stop();
for(auto &i : thr_grp) i.join();
int b;
std::cin >> b;
return 0;
}
回答by Bob Bryan
When the io_service::run method is called without a work object, it will return right away. Typically, that is not the behavior most developers are looking for. There are of course some exceptions, but most developers are looking to specify a thread to handle all of the asynchronous processing and don't want that thread to exit until told to do so. That is what your code example does.
当 io_service::run 方法在没有工作对象的情况下被调用时,它将立即返回。通常,这不是大多数开发人员正在寻找的行为。当然也有一些例外,但大多数开发人员都希望指定一个线程来处理所有异步处理,并且不希望该线程在被告知退出之前退出。这就是您的代码示例所做的。
The io_service::run method is specified as a delegate or function pointer in the create_thread methods. So, when the thread is created from the create_thread method it will call the io_service::run method and it passes the io_service object as an argument. Typically one io_service object can be used with multiple socket objects.
io_service::run 方法在 create_thread 方法中被指定为委托或函数指针。因此,当从 create_thread 方法创建线程时,它将调用 io_service::run 方法并将 io_service 对象作为参数传递。通常,一个 io_service 对象可以与多个套接字对象一起使用。
The stop method is usually called when shutting down the application or when communication between all clients/servers is no longer required and it is not anticipated that any new connections will need to be initiated.
stop 方法通常在关闭应用程序或不再需要所有客户端/服务器之间的通信时调用,并且预计不需要启动任何新连接。