From 365c864a587365fe443b11cc0cd7cfc8f8f8eb81 Mon Sep 17 00:00:00 2001
From: lichao <lichao@aiotlink.com>
Date: 星期二, 01 六月 2021 11:19:22 +0800
Subject: [PATCH] refactor, clean up useless code.
---
box/io_service.cpp | 33 ++++++++-------------------------
1 files changed, 8 insertions(+), 25 deletions(-)
diff --git a/box/io_service.cpp b/box/io_service.cpp
index 1d531e0..5640d50 100644
--- a/box/io_service.cpp
+++ b/box/io_service.cpp
@@ -16,33 +16,16 @@
* =====================================================================================
*/
#include "io_service.h"
-#include <chrono>
-using namespace std::chrono_literals;
-bool IoService::Start()
+IoService::IoService() :
+ guard_(io_.get_executor())
{
- Stop();
- bool cur = false;
- if (!run_.compare_exchange_strong(cur, true)) {
- return false;
- }
-
- auto proc = [this]() {
- while (run_) {
- io_.run_one_for(100ms);
- }
- OnStop();
- };
- std::thread(proc).swap(worker_);
- return true;
+ std::thread([this]() { io_.run(); }).swap(worker_);
}
-
-void IoService::Stop()
+IoService::~IoService()
{
- bool cur = true;
- if (run_.compare_exchange_strong(cur, false)) {
- if (worker_.joinable()) {
- worker_.join();
- }
- }
+ guard_.reset();
+ io_.stop(); // normally not needed, but make sure run() exits.
+ if (worker_.joinable())
+ worker_.join();
}
--
Gitblit v1.8.0