当前位置: 首页 > article >正文

Boost:asio多io_service,多线程run

多io_service,多线程run,相当于多个线程分别处理注册在不同io_service上的回调,也就是每个线程排某个io_service的异步处理:

//mio_mth.cpp
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include <iostream>
#include <chrono>
#include <thread>
#include <functional>
using namespace boost::asio;
using namespace std;
 
thread_local int t_id = 0;
unsigned long getTimestamp()
{
    return std::chrono::system_clock::now().time_since_epoch().count()/std::chrono::system_clock::period::den;
}
 
void timer_handler(int timerID, const boost::system::error_code& err)
{
    if (err)
    {
        cout << getTimestamp() << " Timer cancel" << endl;
        return;
    }
    cout << getTimestamp() <<" t_id:" << t_id << " timerID:" << timerID << " " << " Timer on"<<endl;
    if(t_id == 1)
    {
        this_thread::sleep_for(1s);
    }
    else
    {
        this_thread::sleep_for(2s);
    }
    cout << getTimestamp() <<" t_id:" << t_id << " timerID:" << timerID << " " << " Timer done"<<endl;
}

void threadRunIO(int id, io_service* ios)
{
    t_id = id;
    ios->run();
    cout << getTimestamp() <<" t_id:" << t_id << " exit" << endl;
}

int main()
{
    io_service ios1, ios2;
    cout << getTimestamp() << " Timer enable" << endl;
    deadline_timer t1(ios1, boost::posix_time::seconds(2));
    deadline_timer t2(ios1, boost::posix_time::seconds(2));
    deadline_timer t3(ios1, boost::posix_time::seconds(2));
    deadline_timer t4(ios1, boost::posix_time::seconds(2));
    deadline_timer t5(ios1, boost::posix_time::seconds(2));
    deadline_timer t6(ios2, boost::posix_time::seconds(2));
    t1.async_wait(bind(timer_handler, 1, std::placeholders::_1));
    t2.async_wait(bind(timer_handler, 2, std::placeholders::_1));
    t3.async_wait(bind(timer_handler, 3, std::placeholders::_1));
    t4.async_wait(bind(timer_handler, 4, std::placeholders::_1));
    t5.async_wait(bind(timer_handler, 5, std::placeholders::_1));
    t6.async_wait(bind(timer_handler, 6, std::placeholders::_1));
    thread th1(threadRunIO, 1, &ios1);
    thread th2(threadRunIO, 2, &ios2);
    th1.join();
    th2.join();
    cout << getTimestamp() << " exit" << endl;
    return 0;
}

//g++ -o mm mio_mth.cpp

运行程序输出:

1701937582 Timer enable
1701937584 t_id:1 timerID:1  Timer on
1701937584 t_id:2 timerID:6  Timer on
1701937585 t_id:1 timerID:1  Timer done
1701937585 t_id:1 timerID:2  Timer on
1701937586 t_id:2 timerID:6  Timer done
1701937586 t_id:2 exit
1701937586 t_id:1 timerID:2  Timer done
1701937586 t_id:1 timerID:3  Timer on
1701937587 t_id:1 timerID:3  Timer done
1701937587 t_id:1 timerID:4  Timer on
1701937588 t_id:1 timerID:4  Timer done
1701937588 t_id:1 timerID:5  Timer on
1701937589 t_id:1 timerID:5  Timer done
1701937589 t_id:1 exit
1701937589 exit

可以看到线程2处理timerID:6,与此同时线程1处理timerID:1和timerID:2

然后当线程2处理完timerID:6后,由于没有其他注册在ios2上的回调了,即使他空闲下来了,也不会处理其他timerID,因此线程2会先退出

因为其他的timerID都注册在了ios1上,所以只能等待线程1依次处理。


http://www.kler.cn/news/161583.html

相关文章:

  • Unity中Batching优化的GPU实例化(3)
  • 万户协同办公平台ezoffice wpsservlet接口任意文件上传漏洞
  • uni-app 微信小程序之好看的ui登录页面(四)
  • JM中ref_pic_list_modification bug记录
  • 金融行业文件摆渡,如何兼顾安全和效率?
  • 视频封面提取:精准截图,如何从指定时长中提取某一帧图片
  • 性能优化一条龙
  • Spring Boot的日志
  • 【PyTorch】模型的基本操作
  • html电子签名
  • vue+echarts实现桑吉图的效果
  • 策略模式终极解决方案之策略机
  • Jquery easyui异步提交表单的两种方式
  • Vue练习 v-model 指令在状态和表单输入之间创建双向绑定
  • Vue3集成ThreeJS实现3D效果,threejs+Vite+Vue3+TypeScript 实战课程【一篇文章精通系列】
  • stm32f103使用hal库函数读写内部flash
  • 【分布式微服务专题】从单体到分布式(二、SpringCloud整合Nacos)
  • TR转发路由器测评—云企业网实现跨地域跨VPC的网络互通测评实战【阿里云产品测评】
  • tomcat环境搭建
  • 深入理解Dubbo-1.初识Dubbo
  • Csharp(C#)无标题栏窗体拖动代码
  • 推荐5款很牛的Paas平台编译构建工具
  • .netcore 操作aspose.words导出pdf
  • selenium 执行js后拿不到返回值的原因
  • IT基础监控方案:5台服务器和20台网络设备监控
  • UnityShader自定义cginc文件
  • Intellij idea 快速定位到文件的开头或者结尾的几种方式
  • 预测:2024年的安防监控行业将迎来怎样的变化?
  • 使用postman请求x5接口
  • C语言指针详解上