/*
|
* =====================================================================================
|
*
|
* Filename: io_service.cpp
|
*
|
* Description:
|
*
|
* Version: 1.0
|
* Created: 2021年05月27日 13时25分18秒
|
* Revision: none
|
* Compiler: gcc
|
*
|
* Author: Li Chao (), lichao@aiotlink.com
|
* Organization:
|
*
|
* =====================================================================================
|
*/
|
#include "io_service.h"
|
#include <chrono>
|
using namespace std::chrono_literals;
|
|
bool IoService::Start()
|
{
|
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;
|
}
|
|
void IoService::Stop()
|
{
|
bool cur = true;
|
if (run_.compare_exchange_strong(cur, false)) {
|
if (worker_.joinable()) {
|
worker_.join();
|
}
|
}
|
}
|