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

workflow源码解析:GoTask

关于go task

提供了另一种更简单的使用计算任务的方法,模仿go语言实现的go task。
使用go task来实计算任务无需定义输入与输出,所有数据通过函数参数传递。

与ThreadTask 区别

ThreadTask 是有模板,IN 和 OUT, ThreadTask 依赖 输入输出。

而 GoTask 不依赖,而是直接将函数打包成 go 这个callback,等待线程池消费。

1.示例程序

我们想异步的运行一个加法函数:void add(int a, int b, int& res);
并且我们还想在函数运行结束的时候打印出结果。于是可以这样实现:

#include <stdio.h>
#include <utility>
#include "workflow/WFTaskFactory.h"
#include "workflow/WFFacilities.h"void add(int a, int b, int& res)
{res = a + b;
}int main(void)
{WFFacilities::WaitGroup wait_group(1);int a = 1;int b = 1;int res;WFGoTask *task = WFTaskFactory::create_go_task("test", add, a, b, std::ref(res));task->set_callback([&](WFGoTask *task) {printf("%d + %d = %d\n", a, b, res);wait_group.done();});task->start();wait_group.wait();return 0;
}

2. 继承顺序

在这里插入图片描述

3.源码分析

template<class FUNC, class... ARGS>
inline WFGoTask *WFTaskFactory::create_go_task(const std::string& queue_name,FUNC&& func, ARGS&&... args)
{auto&& tmp = std::bind(std::forward<FUNC>(func),std::forward<ARGS>(args)...);return new __WFGoTask(WFGlobal::get_exec_queue(queue_name),WFGlobal::get_compute_executor(),std::move(tmp));
}
class __WFGoTask : public WFGoTask
{
protected:virtual void execute(){this->go();}protected:std::function<void ()> go;public:__WFGoTask(ExecQueue *queue, Executor *executor,std::function<void ()>&& func) :WFGoTask(queue, executor),go(std::move(func)){}
};

其他和ThreadTask一致,还是通过线程池去执行execute(),从而进行用户函数的执行

4.参考链接

https://github.com/chanchann/workflow_annotation/blob/main/src_analysis/12_go_task.md
https://github.com/sogou/workflow/blob/master/docs/about-go-task.md

http://www.lryc.cn/news/283395.html

相关文章:

  • SpringMVC入门案例
  • Docker本地私有仓库搭建配置指导
  • python 通过定时任务执行pytest case
  • 算法面试题:合并两个有序链表
  • LaWGPT安装和使用教程的复现版本【细节满满】
  • 西门子博途用SCL语言写的入栈出栈
  • 密码产品推介 | 沃通安全电子签章系统(ES-1)
  • 蓝桥杯真题(Python)每日练Day1
  • IDEA怎么用Devtools热部署
  • boost.circular_buffer的使用和介绍
  • 深入理解Java中的ThreadLocal
  • 【重点】【DP】300. 最长递增子序列
  • 使用freessl为网站获取https证书及配置详细步骤
  • Java-初识正则表达式 以及 练习
  • 【Flutter 问题系列第 80 篇】TextField 输入框组件限制可输入的最大长度后,输入的内容中包含表情符号时,获取输入的内容数还是会超出限制的问题
  • 漏洞检测和评估【网站子域扫描工具02】
  • 压力测试+接口测试(工具jmeter)
  • LeetCode 46 全排列
  • npm install 无反应 npm run serve 无反应
  • JAVAEE初阶 文件IO(二)
  • Golang 三数之和+ 四数之和 leetcode15、18 双指针法
  • Mysql三种常用的删除方式
  • Eureka 本机集群实现
  • 查看神经网络中间层特征矩阵及卷积核参数
  • 重置aws上的ssh默认登录端口
  • 算法刷题——拿出最少数目的魔法豆(力扣)
  • Linux消息队列
  • 计算机网络——数据链路层(1)
  • 移动端开发进阶之蓝牙通讯(四)
  • npm换源