The boost ASIO program exits two elegantly

Source: Internet
Author: User

The previous article is very simple and easy to understand. However, in C ++, you still need to encapsulate the code that can be used in multiple projects.

The following cancels the global variable, creates a server class, provides the run function, and the server object also listens to semaphores to exit elegantly.

# Include <cstdlib> # include <boost/ASIO. HPP> # include <boost/bind. HPP> # include <iostream> using namespace boost; using namespace boost: ASIO; using namespace STD; Class server {public: Server (io_service & S): IO _ (s ), signals _ (s) {signals _. add (SIGINT); SIGNALS _. add (sigterm); # If defined (sigquit) signals _. add (sigquit); # endif signals _. async_wait (boost: BIND (& server: Stop, this);} void run () {IO _. run ();} PRIVATE: void stop () {cout <"X" <Endl; IO _. stop () ;}private: io_service & IO _; Boost: ASIO: signal_set signals _ ;}; int main (INT argc, char ** argv) {io_service S; server server (s); server. run (); Return 0 ;}

Run the program and kill the process. The result is that the program exits after X is displayed.

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.