Http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting-started-with-boostasio?pg=4
This section adds tasks for Io_service and distinguishes between dispatch and post. If Io_service is the brain of the ASIO library, Post and dispatch are the hands and feet of the ASIO library.
Let's take a look at example 1
#include <boost/asio.hpp> #include <boost/shared_ptr.hpp> #include <boost/thread.hpp> #include < boost/thread/mutex.hpp> #include <boost/bind.hpp> #include <iostream>boost::mutex global_stream_lock void Workerthread (boost::shared_ptr< boost::asio::io_service > Io_service) {global_stream_lock.lock (); STD:: cout << "[" << boost::this_thread::get_id () << "] Thread Start" << STD::ENDL;GLOBAL_STREAM_ Lock.unlock (); Io_service->run (); Global_stream_lock.lock (); Std::cout << "[" << Boost::this_thread:: get_id () << "] Thread Finish" << std::endl;global_stream_lock.unlock ();} size_t fib (size_t N) {if (n <= 1) {return n;} Boost::this_thread::sleep (boost::p osix_time::milliseconds), return fib (n-1) + fib (n-2); void Calculatefib (size_t n) {global_stream_lock.lock (); Std::cout << "[" << boost::this_thread::get_id () << "] now calculating fib (" << n << ")" << Std::endl; Global_stream_lock.unlock (); size_t f = fib (n); Global_stream_lock.lock () std::cout << "[" << Boost::this_ thread::get_id () << "] fib (" << n << ") =" << f << std::endl;global_stream_lock.unlock ();} int main (int argc, char * argv[]) {boost::shared_ptr< boost::asio::io_service > Io_service (New Boost::asio::io_ser Vice);boost::shared_ptr< boost::asio::io_service::work > work (New Boost::asio::io_service::work (*io_service) ); Global_stream_lock.lock (); Std::cout << "[" << boost::this_thread::get_id () << "] the program would Exit when all work has finished. " << Std::endl;global_stream_lock.unlock (); boost::thread_group worker_threads;for (int x = 0; x < 2; ++x) {Worker_ Threads.create_thread (Boost::bind (&workerthread, Io_service));} Io_service->post (Boost::bind (CALCULATEFIB, 3)); Io_service->post (Boost::bind (CALCULATEFIB, 4)); io_service-& Gt;post (Boost::bind (CALCULATEFIB, 5)); Work.reset (); worker_threads.join_all (); return 0;}
The code uses a smart pointer to control the Io_service, using a mutex to control the output mutex between the various processes. The work class maintains the life cycle of the io_service and then uses post to add execution tasks.
On this basis we look at the difference between Post and dispatch:
The post prioritizes the task into the processing queue and then returns, and the task is completed at some point in time.
Dispatch will immediately request Io_service to invoke the specified task.
Example 2
#include <boost/asio.hpp> #include <boost/shared_ptr.hpp> #include <boost/thread.hpp> #include < boost/thread/mutex.hpp> #include <boost/bind.hpp> #include <iostream>boost::mutex global_stream_lock void Workerthread (boost::shared_ptr< boost::asio::io_service > Io_service) {global_stream_lock.lock (); STD:: cout << "[" << boost::this_thread::get_id () << "] Thread Start" << STD::ENDL;GLOBAL_STREAM_ Lock.unlock (); Io_service->run (); Global_stream_lock.lock (); Std::cout << "[" << Boost::this_thread:: get_id () << "] Thread Finish" << std::endl;global_stream_lock.unlock ();} void Dispatch (int x) {global_stream_lock.lock (); Std::cout << "[" << boost::this_thread::get_id () << " ] "<< __function__ <<" x = "<< x << Std::endl;global_stream_lock.unlock ();} void Post (int x) {global_stream_lock.lock (); Std::cout << "[" << boost::this_thread::get_id () << "]"<< __function__ << "x =" << x << Std::endl;global_stream_lock.unlock ();} void Run3 (boost::shared_ptr< boost::asio::io_service > Io_service) {for (int x = 0; x < 3; ++x) {io_service-> Dispatch (Boost::bind (&dispatch, X * 2)) Io_service->post (Boost::bind (&post, X * 2 + 1)); boost::this_thr Ead::sleep (boost::p osix_time::milliseconds (1000));}} int main (int argc, char * argv[]) {boost::shared_ptr< boost::asio::io_service > Io_service (New Boost::asio::io_ser Vice);boost::shared_ptr< boost::asio::io_service::work > work (New Boost::asio::io_service::work (*io_service) ); Global_stream_lock.lock (); Std::cout << "[" << boost::this_thread::get_id () << "] The program would ex It when all work has finished. "<< Std::endl;global_stream_lock.unlock (); Boost::thread_group worker_threads;for ( int x = 0; x < 1; ++x) {Worker_threads.create_thread (Boost::bind (&workerthread, Io_service));} Io_service->p Ost (Boost::bind (&RUN3, Io_service)); Work.reset (); Worker_threads.join_all (); return 0;}
We can see that the result is showing the results of the dispatch before the post results are displayed, consistent with what was expected.
Boost ASIO Learning (iii)