Created
March 18, 2016 20:42
-
-
Save peetonn/086161b4a9402e4399ca to your computer and use it in GitHub Desktop.
destruction guards?
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
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