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

C++ 引用协程

我们都知道Go的协程非常好用,那么如何在C++里面引入协程呢?

Go 导出协程函数

package main/*
#include <stdio.h>typedef void (__stdcall *PHandleRoutine)(void *params);static void ExecuteRoutine(void* handle, void *params) {PHandleRoutine pRoutine = (PHandleRoutine)handle;if (pRoutine != NULL) {pRoutine(params);}
}
*/
import "C"import ("log""unsafe"
)//export GoRoutine
func GoRoutine(handle unsafe.Pointer, params unsafe.Pointer) {go func() {if r := recover(); r != nil {log.Printf("GoRoutine failed in f %v", r)return}C.ExecuteRoutine(handle, params)}()
}func main() {}

C++ 调用携程

#pragma once
#include <string>
#include <tchar.h>
#include <windows.h>
typedef void(__stdcall* PGoRoutine)(void* handle, void* params);
class XGoRoutineTask
{
public:virtual void Execute() = 0;XGoRoutineTask() { m_bauto_free = true; }virtual ~XGoRoutineTask() {};bool IsAutoFree() { return m_bauto_free; }
protected:bool m_bauto_free;
};class XGoRoutine {
public:static XGoRoutine& Instance(){static XGoRoutine sInstance;return sInstance;}int init(const std::string& dll_path = "GoRoutine.dll"){if (m_hModule != nullptr) return true;m_hModule = ::LoadLibraryA(dll_path.c_str());if (m_hModule == nullptr)return GetLastError();m_PGoRoutine = (PGoRoutine)GetProcAddress(m_hModule,"GoRoutine");return m_PGoRoutine != nullptr;}void Go(XGoRoutineTask* pNewTask){if (m_PGoRoutine)m_PGoRoutine(&XGoRoutine::HandleRoutine, pNewTask);}
private:static void HandleRoutine(void* params){XGoRoutineTask* task = static_cast<XGoRoutineTask*>(params);if (task == nullptr) return;task->Execute();if (task->IsAutoFree()){delete task;task = nullptr;}}XGoRoutine(){m_hModule = nullptr;m_PGoRoutine = nullptr;}XGoRoutine(const XGoRoutine&) = delete;XGoRoutine(const XGoRoutine&&) = delete;
private:HMODULE m_hModule;PGoRoutine m_PGoRoutine;
};

测试


#include <iostream>
#include <windows.h>
#include <tchar.h>
#include "XGoRoutine.h"class XAddTask :public XGoRoutineTask {
public:virtual void Execute() override {printf("XAddTask[%d] excute\r\n",GetCurrentThreadId());}
};class XSubTask :public XGoRoutineTask {
public:virtual void Execute() override {printf("XSubTask[%d] excute\r\n", GetCurrentThreadId());}
};int main()
{XGoRoutine::Instance().init();while (true){XGoRoutine::Instance().Go(new XAddTask());XGoRoutine::Instance().Go(new XSubTask());Sleep(100);}std::cout << "Hello World!\n";
}

在这里插入图片描述

http://www.dtcms.com/a/419312.html

相关文章:

  • 淄博企业网站设计公司网页无法打开怎么办
  • 添加测试设备到苹果开发者平台
  • 填坑:VC++ 采用OpenSSL 3.0接口方式生成RSA密钥
  • 郑州做网站的网站再就业技能培训班
  • Vscode 连接服务时候一直出现setting ssh Host server
  • 全面解析数据库审批平台:主流工具对比与选型指南
  • 【Docker项目实战】使用Docker部署IT运维管理平台CAT
  • spring事务传播级别的实操案例2
  • 泰州专一做淘宝网站如何用html做网站头像
  • 电子商务网站设计与实现个人网站做捐赠发布违法吗
  • Java滑动窗口算法题目练习
  • 介绍一下HTTP和WebSocket的头部信息
  • Linux系统学习之---库的理解和加载(毛坯初版...)
  • 南山模板网站建设公司怎么看网站的外链
  • 企业网站策划大纲模板文山住房和城乡建设局网站
  • Linux 基础IO与系统IO
  • 【IEDA】已解决:IDEA中jdk的版本切换
  • idea推荐springboot+mybatis+分页查询插件之PageHelper
  • 南非网站域名做网站微信支付多少钱
  • 网站开发 图形验证码网站建设衤金手指下拉10
  • OPenssh6代码移植的依赖库 OpenSSL双库连接问题的解决方案
  • 商务网站建设组成包括网站优化wordpress 换行
  • tiktok scheme
  • Xrdp 远程桌面配置【笔记】
  • 【Linux】倒计时和进度条实现
  • 网站建设需要用到哪些软件有哪些系统安装wordpress
  • 梯度下降(Gradient Descent)
  • 东莞市建设规划局网站游戏类企业网站模板
  • C++---bind(绑定函数或函数对象的参数)
  • 网站和域名网站开发技术是什么