asio的异步多线程实现

本文介绍了asio库中如何实现多线程处理io_service,重点解析了task_io_service的run方法和do_run_one方法,阐述了其内部的生产消费者模型。在消费者端,通过task_io_service的run方法调用do_run_one执行任务;在生产者端,以异步写操作为例,展示了如何将任务放入队列并触发处理。整个过程涉及到了io_service、线程同步和回调函数的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用asio的过程中,一个神奇的地方就是io_service能够多线程调用。和之前的muduo库中,oneloop onethread的思想下不太相同。现在的服务器中,io一般只需要一个线程通通搞定,但是计算却需要很多线程保证并发度。所以学习了下asio中如何多个线程,一个loop。其实思想上来说,就是task queue + 多线程,task queue需要不断地加入task和给出task,这就需要加锁。也就是使用了生产消费者模型。

具体来看,消费者端:

task_io_service是非win平台下io_service的impl实现,当io_service->run的时候,就是调用了task_io_service::run

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;
}


其中do_run_one的实现如下:

std::size_t task_io_service::do_run_one(mutex::scoped_lock& lock,
    task_io_service::thread_info& this_thread,
    const boost::system::error_code& ec)
{
  while (!stopped_)
  {
    if (!op_queue_.empty())
    {
      // Prepare to execute first handler from queue.
      operation* o = op_queue_.front();
      op_queue_.pop();
      bool more_handlers = (!op_queue_.empty());


      if (o == &task_operation_)
      {
        task_interrupted_ = more_handlers;


        if (more_handlers && !one_thread_)
          wakeup_event_.unlock_and_signal_one(lock);
        else
          lock.unlock();


        task_cleanup on_exit = { this, &lock, &this_thread };
        (void)on_exit;


        // Run the task. May throw an exception. Only block if the operation
        // queue is empty and we're not polling, otherwise we want to return
        // as soon as possible.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值