关于make_work_guard猜想
猜想:使用make_work_guard有可能是讓io一直保活,不被當(dāng)沒有任務(wù)的時(shí)候,一直輪詢,除非自己調(diào)用stop函數(shù)。歡迎大家留言,這只是我的猜想.
#include<iostream>
#include<boost/asio.hpp>
#include <boost/asio/steady_timer.hpp>
#include <thread>
void print(const boost::system::error_code& e);
boost::asio::io_context ios;
boost::asio::steady_timer heart_timer_(ios);
void print(const boost::system::error_code& ec)
{
?? ?if (ec){
?? ??? ?return;
?? ?}
?? ?std::cout << "Hello!"<<std::endl;
?? ?heart_timer_.expires_from_now(std::chrono::seconds(10));
?? ?heart_timer_.async_wait(std::bind(print, std::placeholders::_1));
}
int main()
{
?? ?int count = 1;
?? ?//heart_timer_.expires_at(std::chrono::steady_clock::now() + std::chrono::seconds(30));
?? ?heart_timer_.async_wait(std::bind(print,std::placeholders::_1));
?? ?auto work_(boost::asio::make_work_guard(ios));
?? ?std::thread t([&]() {
?? ??? ?boost::system::error_code ec;
?? ??? ?ios.run(ec);
?? ??? ?});
?? ?
?? ??? ?while (1) {
?? ??? ??? ?if (count ==10){
?? ??? ??? ??? ?ios.stop();
?? ??? ??? ?}
?? ??? ??? ?++count;
?? ??? ??? ?std::this_thread::sleep_for(std::chrono::seconds(1));
?? ??? ?}
}
總結(jié)
以上是生活随笔為你收集整理的关于make_work_guard猜想的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: filebeat + logstash
- 下一篇: halcon 17 cuda cudnn