Boost.Asio C++ Network Programming - Second Edition by Wisnu Anggoro & John Torjo

Boost.Asio C++ Network Programming - Second Edition by Wisnu Anggoro & John Torjo

Author:Wisnu Anggoro & John Torjo [Anggoro, Wisnu]
Language: eng
Format: azw3
Publisher: Packt Publishing
Published: 2015-09-16T04:00:00+00:00


When we run removework.cpp, compared to blocked.cpp, which will block the program infinitely, the following line of text will be displayed to us:

Now, let's dissect the code. As we can see in the preceding code, we used the shared_ptr pointer to instantiate the work object. With this smart pointer provided by Boost, we no longer need to manually delete memory allocation in order to store the pointer since it guarantees that the object pointed to will be deleted when the last pointer is destroyed or reset. Do not forget to include shared_ptr.hpp inside the boost directory as the shared_ptr pointer is defined in the header file.

We also add the reset() function to reset the io_service object when it prepares for a subsequent run() function invocation. The reset() function has to be invoked before any invocation of the run() or poll() functions. It will also tell the shared_ptr pointer to automatically destroy the pointer we created. More information about the share_ptr pointer can be found at www.boost.org/doc/libs/1_58_0/libs/smart_ptr/shared_ptr.htm.

The preceding program explains that we have successfully removed the work object from the io_service object. We can use this functionality if we intend to finish all the pending work even though it hasn't actually been finished yet.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.