Advertisement
Guest User

Untitled

a guest
Aug 27th, 2014
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.23 KB | None | 0 0
  1.  
  2. #include <iostream>
  3.  
  4. #include <boost/bind.hpp>
  5. #include <boost/ref.hpp>
  6. #include <boost/asio.hpp>
  7.  
  8. void handler(
  9.     boost::asio::signal_set &sset,
  10.     const boost::system::error_code& error,
  11.     int signal_number)
  12. {
  13.     if (!error)
  14.     {
  15.         sset.async_wait(
  16.             boost::bind(
  17.                  handler
  18.                 ,boost::ref(sset)
  19.                 ,boost::asio::placeholders::error
  20.                 ,boost::asio::placeholders::signal_number
  21.             )
  22.         );
  23.         static bool first = true;
  24.         if(first){
  25.             std::cout << " A signal(SIGINT) occurred." << std::endl;
  26.             // do something like writing data to a file
  27.             first = false;
  28.         }else{
  29.             std::cout << " A signal(SIGINT) occurred, exiting...." << std::endl;
  30.             exit(0);
  31.         }
  32.     }
  33. }
  34.  
  35. int main(){
  36.     // Construct a signal set registered for process termination.
  37.     boost::asio::io_service io;
  38.     boost::asio::signal_set signals(io, SIGINT);
  39.     // Start an asynchronous wait for one of the signals to occur.
  40.     signals.async_wait(
  41.         boost::bind(
  42.              handler
  43.             ,boost::ref(signals)
  44.             ,boost::asio::placeholders::error
  45.             ,boost::asio::placeholders::signal_number
  46.         )
  47.     );
  48.  
  49.     io.run();
  50.     size_t i;
  51.     for(i=0;i<std::numeric_limits<size_t>::max();++i){
  52.         // time stepping loop, do some computations
  53.     }
  54.     std::cout << i << std::endl;
  55.     return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement