浅析boost asio中的event loop (3)
来源:互联网 发布:io域名注册 编辑:程序博客网 时间:2024/06/01 03:58
我们再来看看task_io_service的实现
task_io_service提供的最主要的接口如下:
1) run:Run the event loop until interrupted or no more work.
size_t run(boost::system::error_code& ec){ec = boost::system::error_code();//如果没有任务了,直接退出if (outstanding_work_ == 0){ stop(); return 0;}typename call_stack<task_io_service>::context ctx(this);idle_thread_info this_idle_thread;this_idle_thread.next = 0;boost::asio::detail::mutex::scoped_lock lock(mutex_);//循环执行直到完成所有任务队列中的任务size_t n = 0;for (; do_one(lock, &this_idle_thread); lock.lock()) if (n != (std::numeric_limits<size_t>::max)())++n;return n;}
2)poll:Poll for operations without blocking.
size_t poll(boost::system::error_code& ec){//如果没有任务了,直接退出if (outstanding_work_ == 0){ stop(); ec = boost::system::error_code(); return 0;}typename call_stack<task_io_service>::context ctx(this);boost::asio::detail::mutex::scoped_lock lock(mutex_);//循环执行直到完成所有任务队列中已经ready的任务,ready等价于epoll返回size_t n = 0;for (; do_one(lock, 0); lock.lock()) if (n != (std::numeric_limits<size_t>::max)())++n;return n;}
我们可以看到这两个函数最终都是调用do_one函数,差别只是第二个参数
size_t do_one(boost::asio::detail::mutex::scoped_lock& lock, idle_thread_info* this_idle_thread){//this_idle_thread == NULL时是polling模式bool polling = !this_idle_thread;bool task_has_run = false;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());//task_operation_是一个标识符,用于标识执行epoll操作,这里就是task_->runif (o == &task_operation_){ task_interrupted_ = more_handlers || polling; // If the task has already run and we're polling then we're done. if (task_has_run && polling) {task_interrupted_ = true;op_queue_.push(&task_operation_);return 0; } task_has_run = true; if (!more_handlers || !wake_one_idle_thread_and_unlock(lock))lock.unlock(); // c析构时,把completed_ops中的op存入op_queue_中 op_queue<operation> completed_ops; task_cleanup c = { this, &lock, &completed_ops }; (void)c; // 执行epoll,并将ready的op放入completed_ops // 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. task_->run(!more_handlers && !polling, completed_ops);}else{ // 执行op_queue_中的非task_operation_任务 if (more_handlers)wake_one_thread_and_unlock(lock); elselock.unlock(); // Ensure the count of outstanding work is decremented on block exit. work_finished_on_block_exit on_exit = { this }; (void)on_exit; // 执行op的complete接口,这里实际上就是执行 // async_send(const ConstBufferSequence & buffers, WriteHandler handler)中的handler // 后面还会再讲解 // Complete the operation. May throw an exception. o->complete(*this); // deletes the operation object return 1;} } else if (this_idle_thread) { // 如果没有op且不是polling模式的话,线程wait// Nothing to run right now, so just wait for work to do.this_idle_thread->next = first_idle_thread_;first_idle_thread_ = this_idle_thread;this_idle_thread->wakeup_event.clear(lock);this_idle_thread->wakeup_event.wait(lock); } else {return 0; }}return 0;}
- 浅析boost asio中的event loop (3)
- 浅析boost asio中的event loop (1)
- 浅析boost asio中的event loop (2)
- 浅析boost asio中的event loop (4)
- 浅析boost asio中的event loop (5)
- JavaScript Event Loop 浅析
- 浅析boost::asio::deadline_timer运行机制
- Boost.Asio中的线程
- Boost.Asio(二)
- boost-ASIO--(1)
- boost-ASIO--(2)
- boost::asio::error的用法浅析
- Boost::Asio::Error的用法浅析
- Boost::Asio::Error的用法浅析
- c++boost中的asio介绍
- Javascript运行机制中的事件循环(Event Loop)
- 【Boost】boost库asio详解6——boost::asio::error的用法浅析
- 【Boost】boost库asio详解6——boost::asio::error的用法浅析
- lr使用小技巧
- Android signed APK
- 中继器·集线器·网桥·交换机·路由器·网关的区别
- memcached协议及命令
- 用自定义标签实现防盗链的操作
- 浅析boost asio中的event loop (3)
- NSNotificationCenter 的使用
- oracle死锁的解决
- 数据库学习 ORA-12545:因目标主机或对象不存在,连接失败
- Silverlight开发历程—(绘制报表)
- 网络性能测试工具-备忘
- 两个非root用户之间的免密码登录
- 发现自己很弱
- LCUI:Hello World!程序的实现