Created
May 21, 2013 03:41
-
-
Save qxj/5617345 to your computer and use it in GitHub Desktop.
boost samples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <map> | |
#include <boost/asio.hpp> | |
#include <boost/date_time/posix_time/posix_time.hpp> | |
#include <boost/shared_ptr.hpp> | |
#include <boost/bind.hpp> | |
enum { N = 10 }; | |
void print(int i, const boost::system::error_code& error) | |
{ | |
if(!error){ | |
std::cout << "--> timer " << i << std::endl; | |
} | |
} | |
typedef boost::shared_ptr<boost::asio::deadline_timer> timer_ptr; | |
int main(int argc, char *argv[]) | |
{ | |
boost::asio::io_service io; | |
std::map<int, timer_ptr> timers; | |
for (int i = 0; i < N; ++i) { | |
timer_ptr t(new boost::asio::deadline_timer(io)); | |
t->expires_from_now(boost::posix_time::seconds(i)); | |
t->async_wait(boost::bind(print, i, _1)); | |
timers.insert(std::pair<int, timer_ptr>(i, t)); | |
} | |
for (int i = 5; i < N; ++i) { | |
// timers[i]->cancel(); | |
timers.erase(i); | |
} | |
io.run(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment