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

C++中的链式操作原理与应用(三):专注于异步操作延的C++开源库 continuable

目录

1.简介

2.安装与集成

3.快速入门

4.完整示例

5.优势与适用场景


1.简介

        continuable 是一个专注于 异步操作延续(continuation) 的现代 C++ 开源库,旨在简化异步编程流程,解决 “回调地狱” 问题,提供直观、灵活的异步操作链式调用和组合能力。它的设计理念类似于 JavaScript 中的 Promise,但更贴合 C++ 语法特性和现代标准(C++11 及以上)。

        异步编程中,多个依赖的异步操作(如网络请求、文件 IO)常导致嵌套回调(“回调地狱”),代码可读性和维护性极差。continuable 通过 延续传递风格(Continuation-Passing Style) 封装异步操作,允许将异步操作的 “后续处理逻辑”(延续)通过链式调用串联,使异步流程更接近同步代码的线性结构。

        主要特性有:

  1. 链式延续:通过 then() 方法串联异步操作,前一个操作的结果自动传递给下一个延续。
  2. 错误处理:统一的错误传播机制,通过 catch_() 捕获链中任意环节的错误。
  3. 操作组合:支持并行(when_all())、串行(when_seq())等方式组合多个异步操作。
  4. 多范式兼容:可与回调函数、std::future、协程(C++20)等多种异步模型集成。
  5. 轻量级:header-only 库(仅需包含头文件),无外部依赖,易于集成。
  6. 类型安全:通过模板推导自动处理参数类型,编译期检查类型匹配。

2.安装与集成

continuable 是 header-only 库,无需编译,只需:

1.从 GitHub 仓库 下载源码或从网站下载压缩包。

https://github.com/Naios/continuable

2.将 include 目录添加到项目包含路径。

3.包含核心头文件 #include "continuable/continuable.hpp" 即可使用。

支持 C++11 及以上标准,兼容主流编译器(GCC、Clang、MSVC)。

注意:continuable依赖asio和function2库

asio库源码的下载地址:

Downloading Asio

function2的引用可参考:

function2:一个专注于函数对象包装的现代 C++ 开源库-CSDN博客

3.快速入门

1.通过make_continuable创建一个可延续对象,该对象在调用时会返回一个承诺

auto http_request(std::string url) {return cti::make_continuable<std::string>([url = std::move(url)](auto&& promise) {// Perform the actual request through a different library,// resolve the promise upon completion of the task.promise.set_value("<html> ... </html>");// or: promise.set_exception(std::make_exception_ptr(std::exception("Some error")));// or: promise.set_canceled();});
}auto mysql_query(std::string query) {return cti::make_continuable<result_set, bool>([url = std::move(url)](auto&& promise) {//                         ^^^^^^^^^^^^^^ multiple result types});
}auto do_sth() {return cti::make_continuable<void>([](auto&& promise) {//                         ^^^^ no result at all});
}auto run_it() {return async([] {// Directly start with a handler});
}continuable<> run_it() { // With type erasurereturn async([] {});
}

2.通过then附加您的延续,支持多个结果和部分处理程序:

mysql_query("SELECT `id`, `name` FROM `users`").then([](result_set users) {// Return the next continuable to process ...return mysql_query("SELECT `id` name FROM `sessions`");}).then([](result_set sessions) {// ... or pass multiple values to the next callback using tuples or pairs ...return std::make_tuple(std::move(sessions), true);}).then([](result_set sessions, bool is_ok) {// ... or pass a single value to the next callback ...return 10;}).then([](auto value) {//     ^^^^ Templated callbacks are possible too})// ... you may even pass continuables to the `then` method directly:.then(mysql_query("SELECT * `statistics`")).then([](result_set result) {// ...return "Hi";}).then([] /*(std::string result) */ { // Handlers can accept a partial set of arguments{// ...});

3.通过failnext处理故障:

http_request("example.com").then([] {throw std::exception("Some error");}).fail([] (std::exception_ptr ptr) {if (ptr) {try {std::rethrow_exception(ptr);} catch(std::exception const& e) {// Handle the exception or error code here}}});

4.通过特定的执行器调度延续(可能在不同的线程上或稍后)

auto executor = [](auto&& work) {// Dispatch the work here, store it for later invocation or move it to another thread.std::forward<decltype(work)>(work)();
};read_file("entries.csv").then([](Buffer buffer) {// ...}, executor);
//   ^^^^^^^^

5.通过when_allwhen_anywhen_seq连接可延续对象:

// `all` of connections:
(http_request("github.com") && http_request("example.com") && http_request("wikipedia.org")).then([](std::string github, std::string example, std::string wikipedia) {// The callback is called with the response of github,// example and wikipedia.});// `any` of connections:
(http_request("github.com") || http_request("example.com") || http_request("wikipedia.org")).then([](std::string github_or_example_or_wikipedia) {// The callback is called with the first response of either github,// example or wikipedia.});// `sequence` of connections:
(http_request("github.com") >> http_request("example.com") >> http_request("wikipedia.org")).then([](std::string github, std::string example, std::string wikipedia) {// The requests are invoked sequentially});// Mixed logical connections:
(http_request("github.com") && (http_request("example.com") || http_request("wikipedia.org"))).then([](std::string github, std::string example_or_wikipedia) {// The callback is called with the response of github for sure// and the second parameter represents the response of example or wikipedia.});// There are helper functions for connecting continuables:
auto all = cti::when_all(http_request("github.com"), http_request("example.com"));
auto any = cti::when_any(http_request("github.com"), http_request("example.com"));
auto seq = cti::when_seq(http_request("github.com"), http_request("example.com"));

6.通过result处理多个结果变量,并通过recover从故障中恢复:

make_exceptional_continuable<void>(std::make_exception_ptr(std::exception("Some error")).fail([] (std::exception_ptr ptr) {return recover();}).then([] () -> result<> {// We recovered from the failure and proceeding normally// Will yield a default constructed exception type to signal cancellationreturn cancel();});

7.对现有代码进行“promisify”处理,或使用(asio)完成令牌集成:

// Promisification of your existing code that accepts callbacks
auto async_resolve(std::string host, std::string service) {return cti::promisify<asio::ip::udp::resolver::iterator>::from([&](auto&&... args) {resolver_.async_resolve(std::forward<decltype(args)>(args)...);},std::move(host), std::move(service));
}// (boost) asio completion token integration
asio::io_context io_context;
asio::steady_timer steady_timer(io_context);steady_timer.expires_after(std::chrono::seconds(5));
steady_timer.async_wait(cti::use_continuable).then([] {// Is called after 5s});

4.完整示例

continuable 是一个 header-only 库(仅需包含头文件,无需编译库本身),因此使用 CMake 配置项目时,只需指定头文件路径和 C++ 标准即可。以下是详细的 CMake 配置步骤和示例:

1.假设项目结构如下(将 continuable 作为第三方库放在项目中):

2.CMakeLists.txt 配置

核心配置包括:指定 C++ 标准(需 C++11 及以上)、添加 continuable 的头文件路径。

# 最低CMake版本要求
cmake_minimum_required(VERSION 3.10)# 项目名称和版本
project(continuable_demo VERSION 1.0)# 指定C++标准(continuable需要C++11及以上)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF) # 禁用编译器扩展# 添加continuable的头文件路径
# 假设continuable放在third_party目录下
#target_include_directories(continuable_demo
#  PRIVATE
#    ${CMAKE_CURRENT_SOURCE_DIR}/third_party/continuable/include
#)message(STATUS "continuable directories: ${CMAKE_CURRENT_SOURCE_DIR}")include_directories(${CMAKE_CURRENT_SOURCE_DIR}/third_party/include${CMAKE_CURRENT_SOURCE_DIR}/third_party/include/dep/function2/include
)# 添加源文件
add_executable(continuable_demosrc/main.cpp
)

3.示例代码(src/main.cpp)

编写一个简单的异步操作示例,验证配置是否正确:

#include <iostream>
#include <thread>
#include <chrono>
// 引入continuable头文件
#include "continuable/continuable.hpp"// 模拟异步操作:1秒后返回一个整数
auto async_operation(int value) {return cti::make_continuable<int>([value](auto&& promise) {// 在新线程中执行异步操作std::thread([value, promise = std::forward<decltype(promise)>(promise)]() mutable {std::this_thread::sleep_for(std::chrono::seconds(1)); // 模拟耗时操作promise.set_value(value * 2); // 异步操作结果}).detach();});
}int main() {std::cout << "开始异步操作..." << std::endl;// 链式调用异步操作async_operation(5).then([](int result) {std::cout << "第一步结果: " << result << "(5*2)" << std::endl;return async_operation(result); // 传递结果到下一个异步操作}).then([](int result) {std::cout << "第二步结果: " << result << "(10*2)" << std::endl;})/*.catch_([](const std::exception& e) {std::cerr << "错误: " << e.what() << std::endl;})*/;// 等待异步操作完成(实际项目中用事件循环替代)std::this_thread::sleep_for(std::chrono::seconds(3));return 0;
}

4.编译步骤

mkdir build && cd build
cmake ..          # 生成Makefile
cmake --build . --config Debug             # 编译项目
.\Debug\continuable_demo # 运行程序

5.关键说明

1)header-only 特性

continuable 无需编译为静态库或动态库,只需在 CMake 中通过 target_include_directories 指定其头文件路径即可。

2)C++ 标准

必须指定 CMAKE_CXX_STANDARD 11 或更高(如 C++14、C++17),否则会因语法不兼容导致编译错误。

3)跨平台兼容性

上述配置兼容 Windows(MSVC)、Linux(GCC/Clang)、macOS,只需确保编译器支持 C++11 及以上标准。

6.完整源码下载地址

通过网盘分享的文件:continuableTest.zip
链接: https://pan.baidu.com/s/1MkrlvYChgIWr2boGnwD4_g?pwd=1234 提取码: 1234

5.优势与适用场景

  • 简化异步流程:将嵌套回调转为线性链式调用,代码更易读、维护。
  • 灵活组合:支持串行、并行等复杂异步工作流,满足多任务依赖场景。
  • 现代 C++ 友好:充分利用模板、lambda、移动语义等特性,类型安全且高效。

适用于需要处理大量异步操作的场景:网络编程(如 HTTP 请求、RPC 调用)、文件 IO、异步数据库操作等。

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

相关文章:

  • 决策树 >> 随机森林
  • 智慧工地从工具叠加到全要素重构的核心引擎
  • Claude Code频繁出错怎么办?深入架构层面的故障排除指南
  • 【Linux学习|黑马笔记|Day4】IP地址、主机名、网络请求、下载、端口、进程管理、主机状态监控、环境变量、文件的上传和下载、压缩和解压
  • 【论文阅读】基于表面肌电信号的下肢多关节运动估计:一种深度卷积神经网络方法
  • [小练习]生成54张扑克牌,洗牌。
  • 解决 VSCode 运行 Python 时 ModuleNotFoundError: No module named ‘open_webui‘ 问题
  • 三角洲知识点
  • CI/CD流水线搭建流程
  • 药房发药的“时间密码”:同步时钟用药安全?
  • 抗辐照CANFD通信芯片在高安全领域国产化替代的研究
  • CMake进阶: externalproject_add用于在构建阶段下载、配置、构建和安装外部项目
  • 常见的Jmeter压测问题
  • 飞算 JavaAI 云原生实践:基于 Docker 与 K8s 的自动化部署架构解析
  • 用架构建模工具Sparx EA绘制企业转型路线图
  • C++状态模式详解:从OpenBMC源码看架构、原理与应用
  • NineData云原生智能数据管理平台新功能发布|2025年7月版
  • 云原生俱乐部-k8s知识点归纳(2)
  • 生产环境中Debezium CDC与Kafka实时流处理实战指南
  • 3ds MAX文件/贴图名称乱码?6大根源及解决方案
  • .NET 在鸿蒙系统(HarmonyOS Next)上的适配探索与实践
  • 界面设计风格解析 | ABB 3D社交媒体视觉效果设计
  • 【力扣56】合并区间
  • 一种适用于 3D 低剂量和少视角心脏单光子发射计算机断层成像(SPECT)的可泛化扩散框架|文献速递-深度学习人工智能医疗图像
  • MTK平台Wi-Fi学习--wifi channel 通过国家码进行功率限制和wifi eFEM 基本配置和wifi Tx SEM问题
  • 【深度学习】深度学习的四个核心步骤:从房价预测看机器学习本质
  • Navicat 全量增量数据库迁移
  • 【经验分享】如何在Vscode的Jupyter Notebook中设置默认显示行号
  • OpenCv(三)——图像平滑处理
  • dockerfile示例