Commit 09092e0b authored by octal's avatar octal

Improved the LoadMonitor

parent 3ae548f8
...@@ -70,10 +70,16 @@ struct LoadMonitor { ...@@ -70,10 +70,16 @@ struct LoadMonitor {
} }
void start() { void start() {
shutdown_ = false;
thread.reset(new std::thread(std::bind(&LoadMonitor::run, this))); thread.reset(new std::thread(std::bind(&LoadMonitor::run, this)));
} }
void shutdown() {
shutdown_ = true;
}
~LoadMonitor() { ~LoadMonitor() {
shutdown_ = true;
thread->join(); thread->join();
} }
...@@ -82,9 +88,13 @@ private: ...@@ -82,9 +88,13 @@ private:
std::unique_ptr<std::thread> thread; std::unique_ptr<std::thread> thread;
std::chrono::seconds interval; std::chrono::seconds interval;
std::atomic<bool> shutdown_;
void run() { void run() {
Net::Tcp::Listener::Load old; Net::Tcp::Listener::Load old;
while (endpoint_->isBound()) { while (!shutdown_) {
if (!endpoint_->isBound()) continue;
endpoint_->requestLoad(old).then([&](const Net::Tcp::Listener::Load& load) { endpoint_->requestLoad(old).then([&](const Net::Tcp::Listener::Load& load) {
old = load; old = load;
...@@ -134,4 +144,5 @@ int main(int argc, char *argv[]) { ...@@ -134,4 +144,5 @@ int main(int argc, char *argv[]) {
monitor.start(); monitor.start();
server->serve(); server->serve();
monitor.shutdown();
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment