I have the following program, taken from http://www.boost.org/doc/libs/1_65_1/doc/html/boost_asio/tutorial/tuttimer5.html
#include <iostream>
#include <boost/asio.hpp>
#include <boost/thread/thread.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
class Printer {
private:
boost::asio::io_service::strand strand_;
boost::asio::deadline_timer timer1_;
boost::asio::deadline_timer timer2_;
int count_;
public:
Printer(boost::asio::io_service& io) : strand_(io), timer1_(io, boost::posix_time::seconds(1)), timer2_(io, boost::posix_time::seconds(1)), count_(0) {
timer1_.async_wait(strand_.wrap(boost::bind(&Printer::print1, this)));
timer2_.async_wait(strand_.wrap(boost::bind(&Printer::print2, this)));
}
~Printer() {
std::cout << "Final count is " << count_ << std::endl;
}
void print1() {
if(count_ < 10) {
std::cout << "Timer 1: " << count_ << std::endl;
++count_;
timer1_.expires_at(timer1_.expires_at() + boost::posix_time::seconds(1));
timer1_.async_wait(strand_.wrap(boost::bind(&Printer::print1, this)));
}
}
void print2() {
if(count_ < 10) {
std::cout << "Timer 2: " << count_ << std::endl;
++count_;
timer2_.expires_at(timer2_.expires_at() + boost::posix_time::seconds(1));
timer2_.async_wait(strand_.wrap(boost::bind(&Printer::print2, this)));
}
}
};
int main() {
boost::asio::io_service io;
Printer p(io);
boost::thread t(boost::bind(&boost::asio::io_service::run, &io));
io.run();
t.join();
return 0;
}
As far as I understand,
timer1_.async_wait(strand_.wrap(boost::bind(&Printer::print1, this)));
timer2_.async_wait(strand_.wrap(boost::bind(&Printer::print2, this)));
ensures that print1
and print2
will never be run concurrently.
However, what I don't understand is how things are settled in the main
function.
I assume that
boost::thread t(boost::bind(&boost::asio::io_service::run, &io));
creates a new thread in addition to the main one io.run()
and that
t.join()
blocks the main thread until t
completes.
What I don't understand is, what work is assigned to thread t
and what work is assigned to the main thread.
Moreoever, what's the point of having two async_wait
in two different threads?
Thanks,