Skip to content

Instantly share code, notes, and snippets.

@peetonn
Created March 18, 2016 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peetonn/086161b4a9402e4399ca to your computer and use it in GitHub Desktop.
Save peetonn/086161b4a9402e4399ca to your computer and use it in GitHub Desktop.
destruction guards?
class Periodic {
public:
Periodic(io_service& io):io_(io), timer_(io){}
// thread-unsafe
void addName(string name){
names_.push_back(name);
}
// thread-safe
void addNameTS(string name){
io_.dispatch([this](){
names_.push_back(name);
});
}
void start(){ setupTimer(30); }
private:
io_service& io_;
steady_timer timer_;
vector<string> names_;
void setupTimer(double rate)
{
timer_.expires_from_now(milliseconds(1000/rate));
timer_.async_wait([this](error_code& code){
if (code != error::operation_aborted)
{
printNames();
setupTimer();
}
});
}
void printNames()
{
for (auto name:names_)
cout << "hello, " << name << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment