There is an example of using boost::asio.
srv.run ();
not called to perform tasks in the threads?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?
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;
}
At its core, Boost Asio provides a task execution framework that you can use to perform operations of any kind. You create your tasks as function objects and post them to a task queue maintained by Boost Asio. You enlist one or more threads to pick these tasks (function objects) and invoke them.
io_service::run (1 of 2 overloads) Multiple threads may call the run() function to set up a pool of threads from which the io_service may execute handlers. All threads that are waiting in the pool are equivalent and the io_service may choose any one of them to invoke a handler.
The io_service class provides the core I/O functionality for users of the asynchronous I/O objects, including: boost::asio::ip::tcp::socket. boost::asio::ip::tcp::acceptor. boost::asio::ip::udp::socket. deadline_timer .
Asio's io_service is the facilitator for operating on asynchronous functions. Once an async operation is ready, it uses one of io_service 's running threads to call you back. If no such thread exists it uses its own internal thread to call you. Think of it as a queue containing operations.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With