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

asio之task_io_service多线程

简介

task_io_service运行在多线程情况时,call_stack来管理运行io_service的多个线程

结构

call_stack<Key, Value>
- tss_ptr<context> top_
+Value* contains(Key* k)
+Value* top()
call_stack::context
- Key* key_
- Value* value_
- context* next_
task_io_service_thread_info
+ op_queue<task_io_service_operation> private_op_queue
+ long private_outstanding_work
thread_info_base

其中key为task_io_service,value为task_io_service_thread_info

初始化

是在task_io_service::run中,创建task_io_service_thread_info的局部变量(循环没有退出时一直存在),同时添加到call_stack的栈顶

std::size_t task_io_service::run(boost::system::error_code& ec)
{ec = boost::system::error_code();if (outstanding_work_ == 0){stop();return 0;}thread_info this_thread;this_thread.private_outstanding_work = 0;thread_call_stack::context ctx(this, this_thread);mutex::scoped_lock lock(mutex_);std::size_t n = 0;for (; do_run_one(lock, this_thread, ec); lock.lock())if (n != (std::numeric_limits<std::size_t>::max)())++n;return n;
}

使用

在任务分发时,如果当前调用线程是io线程,则直接调用,否则放入对应的队列中

template <typename Handler>
void task_io_service::dispatch(Handler& handler)
{if (thread_call_stack::contains(this)){fenced_block b(fenced_block::full);boost_asio_handler_invoke_helpers::invoke(handler, handler);}else{// Allocate and construct an operation to wrap the handler.typedef completion_handler<Handler> op;typename op::ptr p = { boost::asio::detail::addressof(handler),boost_asio_handler_alloc_helpers::allocate(sizeof(op), handler), 0 };p.p = new (p.v) op(handler);BOOST_ASIO_HANDLER_CREATION((p.p, "io_service", this, "dispatch"));do_dispatch(p.p);p.v = p.p = 0;}
}

http://www.mrgr.cn/news/10435.html

相关文章:

  • [Linux]如何在虚拟机安装Ubuntu?(小白向)
  • Java获取List实体交集差集
  • docker inspect输出内容详解,推测容器运行命令
  • 【案例59】WebSphere类加载跟踪开启方法
  • 吴恩达谈AI未来:Agentic Workflow、推理成本下降与开源的优势
  • 【Kotlin设计模式】Kotlin实现工厂模式
  • Python-断点续传的方式下载GPM降水数据
  • 企业高性能web服务器知识点合集
  • [指南]微软发布Windows-Linux双系统无法启动的完整修复方案
  • 可变参数模板(C++11)
  • 深度学习设计模式之策略模式
  • 【HTML】DOCTYPE作用
  • 【clickhouse】访问clickhouse数据库,并且插入数据
  • vue3+elementPlus:无法清空问题,清空表单没效果
  • Clearpool 推出 Ozean:专注 RWA 的高性能创新区块链
  • 使用 QML 类型系统注册 C++ 类型
  • Threejs三要素及demo
  • 高标准农田灌区对农业发展的支撑作用
  • 你没有使用过(但应该使用)的前 5 个 HTML 功能
  • 8.20-ansible剧本的使用+roles的应用