Last active
October 6, 2015 05:07
-
-
Save legnaleurc/2940673 to your computer and use it in GitHub Desktop.
This file contains 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 <chrono> | |
#include <queue> | |
#include <boost/asio/io_service.hpp> | |
#include <boost/asio/steady_timer.hpp> | |
#include <boost/asio/strand.hpp> | |
class Runner { | |
public: | |
Runner (): | |
id(0), | |
jq(), | |
ios(), | |
lock(ios), | |
pi(2), | |
producer(this->ios, this->pi), | |
ci(3), | |
consumer(this->ios, this->ci) { | |
} | |
void exec () { | |
this->producer.async_wait(this->lock.wrap(std::bind(&Runner::ph, this, std::placeholders::_1))); | |
this->consumer.async_wait(this->lock.wrap(std::bind(&Runner::ch, this, std::placeholders::_1))); | |
this->ios.run(); | |
} | |
private: | |
void ph (const boost::system::error_code & /*ec*/) { | |
int tmp = this->id++; | |
this->jq.push(tmp); | |
std::cout << "push " << tmp << std::endl; | |
this->producer.expires_from_now(this->pi); | |
this->producer.async_wait(this->lock.wrap(std::bind(&Runner::ph, this, std::placeholders::_1))); | |
} | |
void ch (const boost::system::error_code & /*ec*/) { | |
while (!this->jq.empty()) { | |
int tmp = this->jq.front(); | |
this->jq.pop(); | |
std::cout << "pop " << tmp << std::endl; | |
} | |
this->consumer.expires_from_now(this->ci); | |
this->consumer.async_wait(this->lock.wrap(std::bind(&Runner::ch, this, std::placeholders::_1))); | |
} | |
int id; | |
std::queue<int> jq; | |
boost::asio::io_service ios; | |
boost::asio::strand lock; | |
std::chrono::seconds pi; | |
boost::asio::steady_timer producer; | |
std::chrono::seconds ci; | |
boost::asio::steady_timer consumer; | |
}; | |
int main(int argc, char **argv) { | |
Runner runner; | |
runner.exec(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment