We need to install boost library on Ubuntu:
$ $ cat /proc/version Linux version 3.11.0-12-generic (buildd@allspice) (gcc version 4.8.1 (Ubuntu/Linaro 4.8.1-10ubuntu7) ) #19-Ubuntu SMP Wed Oct 9 16:20:46 UTC 2013 $ sudo apt-get install libboost-all-dev
Here is the list of installed boost libraries:
libboost_atomic libboost_chrono libboost_context libboost_date_time libboost_filesystem libboost_graph_parallel libboost_graph libboost_iostreams libboost_locale libboost_math_c99f libboost_math_c99l libboost_math_c99 libboost_math_tr1f libboost_math_tr1l libboost_math_tr1 libboost_mpi_python-py27 libboost_mpi_python-py33 libboost_mpi_python libboost_mpi libboost_prg_exec_monitor libboost_program_options libboost_python-py27 libboost_python-py33 libboost_python libboost_random libboost_regex libboost_serialization libboost_signals libboost_system libboost_thread libboost_timer libboost_unit_test_framework libboost_wave libboost_wserialization
The asio classes can be used by including the asio.hpp header file.
The following example will print out "Blocking wait()..." in 0, 1, 2, 3, and 4 second intervals by using a timer synchronously. That is, the call to wait() will not return until the timer has expired.
#include <iostream> #include <boost/asio.hpp> #include <boost/date_time/posix_time/posix_time.hpp> int main() { boost::asio::io_service io; for(int i = 0; i < 5; i++) { boost::asio::deadline_timer timer(io, boost::posix_time::seconds(i)); timer.wait(); std::cout << "Blocking wait(): " << i << " second-wait\n"; } return 0; }
Makefile looks like this:
sync_timer: sync_timer.o g++ -o sync_timer sync_timer.o -lboost_system -lboost_thread -lpthread sync_timer.o: sync_timer.cpp g++ -c sync_timer.cpp clean: rm -f *.o sync_timer
Output:
$ make g++ -c sync_timer.cpp g++ -o sync_timer sync_timer.o -L /usr/lib -lboost_system -lboost_thread -lpthread $ ./sync_timer Blocking wait(): 0 second-wait Blocking wait(): 1 second-wait Blocking wait(): 2 second-wait Blocking wait(): 3 second-wait Blocking wait(): 4 second-wait
boost::asio::io_service io;
boost::asio::deadline_timer t(io, boost::posix_time::seconds(i));
The following code demonstrates how to use asio's asynchronous callback functionality and how to perform an asynchronous wait on the timer.
#include <iostream> #include <boost/asio.hpp> #include <boost/date_time/posix_time/posix_time.hpp> void work_for_io_service(const boost::system::error_code& /*e*/) { std::cout << "Non-blocking wait() \n"; } int main() { boost::asio::io_service io; boost::asio::deadline_timer timer(io, boost::posix_time::seconds(5)); // work_for_io_service() will be called // when async operation (async_wait()) finishes // note: Though the async_wait() immediately returns // but the callback function will be called when time expires timer.async_wait(&work_for_io_service); std::cout << "If we see this before the callback function, we know async_wait() returns immediately\n. This confirms async_wait() is non-blocking call!\n"; // the callback function, work_for_io_service(), will be called // from the thread where io.run() is running. io.run(); return 0; }