当前位置:网站首页>libuv异步任务逻辑和uv_queue_work()
libuv异步任务逻辑和uv_queue_work()
2022-06-22 02:56:00 【叫我小黄吧】
前言
libuv没有asio的post函数,但我们可以uv_async_init+uv_async_send模拟,uv_async_send传参可以借助 async.data。由于某些任务很耗时(如文件读写,或者连接mysql),如果直接通过上面这种方式添加到eventloop中,会影响eventloop的吞吐量,所以libuv提供了uv_queue_work来解决这种情况。
libuv的异步任务逻辑
这部分其实就是uv_loop_run的延伸,就不逐代码分析了直接上流程图。

uv_queue_work(loop,callback, done)
libuv提供了线程池。为了解决某些异步任务耗时过长会影响eventloop的吞吐率问题,我们可以将这种任务分成两部分:callback为耗时逻辑 ,done 为执行完callback的后续逻辑。callback交给线程池完成,完成后立刻添加done到eventloop中,然后唤醒eventloop,调用uv_work_done函数,调用done函数。
uv_queue_work 的work req添加和回调触发的详细流程图

在这我更能体会到req和handle的区别,handle初始化后就一直存在在loop中,我们需要调用close将其从loop中移除,但req则不需要,在添加到线程池工作队列中后,在执行一次后,就会将其从工作队列中移除,done部分也是如此(从loop的wq中移除)。
边栏推荐
- C3-qt realize Gobang games (I) November 7, 2021
- 使用开源软件攒一个企业级图数据平台解决方案
- Horizontal comparison of domestic API management platforms, which one is stronger?
- [Shangshui Shuo series] day two
- ZCMU--1052: Holedox Eating(C语言)
- linux下安装mysql8及使用(转载)
- Implementation differences between import and require in browser and node environments
- 最新發布:Neo4j 圖數據科學 GDS 2.0 和 AuraDS GA
- Sword finger offer 57 Next node of binary tree
- PMP pre exam guide on June 25, you need to do these well
猜你喜欢

圖數據庫ONgDB Release v-1.0.2

【2. 归并排序】
![[percona toolkit] series of Pt table checksum and Pt table sync data verification and repair artifacts](/img/1f/89cc2ec9ae6c967791994acac55c04.png)
[percona toolkit] series of Pt table checksum and Pt table sync data verification and repair artifacts

C++ primer Chapter 2 summary of variables and basic types

Microsoft Internet Explorer was permanently closed on June 15

Must the database primary key be self incremented? What scenarios do not suggest self augmentation?

Day14QProgressBar2021-10-17

Figure base de données ongdb version V - 1.0.2

Flink CDC MongoDB Connector 的实现原理和使用实践

最新發布:Neo4j 圖數據科學 GDS 2.0 和 AuraDS GA
随机推荐
使用 Neo4j 沙箱学习 Neo4j 图数据科学 GDS
Day13QMainWindow2021-09-28
六、MySQL之数据定义语言(一)
【6. 高精度乘法】
最热门海量的阿里云盘资源分享
xpm_memory_tdpram原语的完整使用实例
Extending kubernetes API with CRD
Write the processing framework for playing
[1. quick sort]
背光模组的基本结构与应用
【8、一维前缀和】
Day12QFile2021-09-27
【2. 归并排序】
B-Tree
PMP pre exam guide on June 25, you need to do these well
The neo4j skill tree was officially released to help you easily master the neo4j map database
fatal error: png++/png. Hpp: no that file or directory
Typora + picGo 配置图床实现图片自动上传
Dynamically load assetdatabase of assets
Figure base de données ongdb version V - 1.0.2