Boost ASIO learning (v) Error handling

Source: Internet
Author: User

Http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting-started-with-boostasio?pg=6

5. Error Handling

The next topic we need to pay attention to is error handling. In other words, what happens when a function throws an exception

Boost::asio gives the user two options to handle. Errors are propagated through handler, indicating where the thread calls the run or poll series functions. The user can handle the state thrown through the exception or receive the returned error variable. For more information on boost, you can refer to the boost error and exception handling.

First, let's look at the method of exception handling error

#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 (); Try{io_service->run ();} catch (Std::exception & ex) {Global_stream_lock.lock (); Std::cout << "[" << boost::this_thread::get_id ( ) << "] Exception:" << ex.what () << std::endl;global_stream_lock.unlock ();} Global_stream_lock.lock (); Std::cout << "[" << boost::this_thread::get_id () << "] Thread Finish" < < Std::endl;global_stream_lock.unlock ();} void Raiseanexception (boost::shared_ptr< boost::asio::io_service > Io_service) {global_stream_lock.lock (); STD :: cout <<"[" << boost::this_thread::get_id () << "]" << __function__ << STD::ENDL;GLOBAL_STREAM_ Lock.unlock (); Io_service->post (Boost::bind (&raiseanexception, Io_service)); Throw (Std::runtime_error (" Oops! ") );} 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 (&raiseanexception, Io_service)); Worker_threads.join_all (); return 0;}

  

In this example, the work thread exits because the exception is freed by the run function. After all threads exit, the program ends due to the return of the Join_all.

Here's an example of using an error variable to return an exception

#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 (); Boost::system::error_code Ec;io_service->run (EC), if (EC) {Global_stream_lock.lock (); std::cout << "[" << boost::this_thread::get_id () << "] Exception:" << EC << STD::ENDL;GLOBAL_STREAM_ Lock.unlock ();} Global_stream_lock.lock (); Std::cout << "[" << boost::this_thread::get_id () << "] Thread Finish" < < Std::endl;global_stream_lock.unlock ();} void Raiseanexception (boost::shared_ptr< boost::asio::io_service > Io_service) {global_stream_lock.lock (); STD :: cout << "[" << boost::this_thread::get_id () << "]" << __function__ << Std::endl;global_stream_lock.unlock (); Io_service->post (Boost::bind (&raiseanexception, Io_service)); Throw (Std::runtime_error ("Oops!"));} 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 (&raiseanexception, Io_service)); Worker_threads.join_all (); return 0;}

  

The above code will cause the program to crash. By debugging, we can find that the thrown exception is not handled

Correctly handle the following

#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 (); while (true) {Try{boost::system::error_code ec;io_service->run (EC); if (EC) {GLOBAL_STREAM_ Lock.lock (); Std::cout << "[" << boost::this_thread::get_id () << "] Error:" << EC << Std::en Dl;global_stream_lock.unlock ();} break;} catch (Std::exception & ex) {Global_stream_lock.lock (); Std::cout << "[" << boost::this_thread::get_id ( ) << "] Exception:" << ex.what () << std::endl;global_stream_lock.unlock ();}} Global_stream_lock.lock (); Std::cout << "[" << Boost::this_thread::get_id () << "] Thread Finish" << std::endl;global_stream_lock.unlock ();} void Raiseanexception (boost::shared_ptr< boost::asio::io_service > Io_service) {global_stream_lock.lock (); STD :: cout << "[" << boost::this_thread::get_id () << "]" << __function__ << Std::endl;global_ Stream_lock.unlock (); Io_service->post (Boost::bind (&raiseanexception, Io_service)); throw (std::runtime_ Error ("oops!"));} int main (int argc, char * argv[]) {boost::shared_ptr< boost::asio::io_service > Io_service (New Boost::asio::io_ Service);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 (&raiseanexception, Io_service)); Worker_threads.join_all (); return 0;}

  

Boost ASIO learning (v) Error handling

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.