Skip to content

Commit

Permalink
add boost::asio timer example/test code
Browse files Browse the repository at this point in the history
  • Loading branch information
zacheryph committed Dec 6, 2008
1 parent 41cf0ce commit 5fd4b95
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions c-cpp/boost_test.cc
@@ -0,0 +1,55 @@
/* boost_test.cc: example boost::asio timer code.
*
* Mac OS X (boost installed in MacPorts)
* g++ -o bt -I/opt/local/include boost_test.cc -L/opt/local/lib -lboost_system-mt
*
* Most other Systems:
* g++ -o bt boost_test.cc -lboost_system-mt
*/
#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>


/* handler object */
class printer
{
public:
printer(boost::asio::io_service &io):
timer_(io, boost::posix_time::seconds(2)),
count_(0)
{
timer_.async_wait(boost::bind(&printer::print, this));
}

~printer()
{
std::cout << "Final Count: " << count_ << ".\n";
}

void print()
{
if (count_ < 5) {
std::cout << "running: " << count_++ << ".\n";

timer_.expires_at(timer_.expires_at() + boost::posix_time::seconds(2));
timer_.async_wait(boost::bind(&printer::print, this));
}
}

private:
boost::asio::deadline_timer timer_;
int count_;
};


int main (int argc, char const **argv)
{
boost::asio::io_service ios;
printer p(ios);

ios.run();

return 0;
}

0 comments on commit 5fd4b95

Please sign in to comment.