asio 0.1.5 | Main Page | Class Index | Member Index | Tutorial |
Step 1. We start by including the necessary header files. All of the asio classes can be used by simply including the "asio.hpp"
header file.
#include <iostream> #include "asio.hpp"
Step 2. All programs that use asio need to have at least one asio::demuxer object. This class provides access to the event demultiplexing functionality, but more on what that actually means later. We declare an object of this type first thing in the main function.
int main() { asio::demuxer d;
Step 3. Next we declare an object of type asio::timer. The core asio classes that provide I/O functionality (or as in this case timer functionality) always take a reference to a demuxer as their first constructor argument. The remaining arguments to the constructor set the timer to expire 5 seconds from now.
asio::timer t(d, asio::timer::from_now, 5);
Step 4. In this simple example we perform a blocking wait on the timer. That is, the call to wait() will not return until the timer has expired, 5 seconds after it was created.
t.wait();
Step 5. Finally we print the obligatory "Hello, world!"
message to show when the timer has expired.
std::cout << "Hello, world!\n"; return 0; }
See the Source listing for Tutorial Part 1
Return to the Tutorial Index
Go on to Tutorial Part 2 - Using a timer asynchronously