06.2_c/c++开源库boost_coroutine2 协程库

1.安装与说明

安装

sudo apt install libboost-coroutine1.71-dev

编译链接

libboost-coroutine不支持.pc格式查看, 支持.cmake导入

cat /usr/lib/x86_64-linux-gnu/cmake/boost_coroutine-1.71.0/boost_coroutine-config.cmake

cat /usr/lib/x86_64-linux-gnu/cmake/boost_coroutine-1.71.0/libboost_coroutine-variant-shared.cmake

cmake 复制代码
list(APPEND _BOOST_COROUTINE_DEPS atomic chrono context thread headers)

可知依赖: atomic chrono context thread

从安装路径可知

头文件路径: /usr/include/boost/coroutine2/coroutine.hpp

/usr/include/boost/coroutine2/all.hpp

库文件路径: /usr/lib/x86_64-linux-gnu/libboost_coroutine.so

故从路径可知
编译选项:
链接选项: -lboost_coroutine -lboost_context

2.实例

1.代码

1_boost.coroutine2协程实例.cc

c++ 复制代码
#include <iostream>
#include <boost/coroutine2/all.hpp>

// 协程函数,接收一个 sink 对象作为参数,用于输出数据
void fibonacci(boost::coroutines2::coroutine<int>::push_type& sink) {
    int a = 0, b = 1;
    while (true) 
    {
        sink(a);
        std::tie(a, b) = std::make_tuple(b, a + b);
        if (a > 100) break; // 限制输出前10个斐波那契数
    }
}

int main() {
    // 创建一个 pull 类型的协程对象,它将调用 fibonacci 函数
    boost::coroutines2::coroutine<int>::pull_type source(fibonacci);

    // 从协程中拉取并打印斐波那契数
    while (source) 
    {
        std::cout << "Main: " << source.get() << std::endl;
        source();
    }

    std::cout << std::endl;

    return 0;
}

2_boost.coroutine2_协程.cc

c++ 复制代码
#include<iostream>
#include<boost/coroutine2/coroutine.hpp>

using namespace boost::coroutines2;

void coro_function(coroutine<int>::push_type& sink) {
    for (int i = 0; i < 5; ++i)
    {
        std::cout << "Coroutine: " << i << std::endl;
        sink(i);
    }
}

int main() {
    coroutine<int>::pull_type source(coro_function);

    while (source)
    {
        std::cout << "Main: " << source.get() << std::endl;
        source();
    }

    return 0;
}

2.scons构建

SConstruct

python 复制代码
## 模板1
import os
env = Environment()
env["PROGSUFFIX"] = ".out"              # 可执行文件后缀.out
env["CCFLAGS"] = " -g3 -O0 -Wall"       # gdb 调试
env["LIBS"] = ["boost_coroutine","boost_context"]

sources=0
ccflags=1
linkflags=2
target_list = [
    "1_boost.coroutine2协程实例.cc",
    "2_boost.coroutine2_协程.cc",
]
for target in target_list:
    if isinstance(target,(str)):
        env.Program(Split(target))
    elif isinstance(target,(list)):
        env.Program(Split(target[sources]),CCFLAGS=target[ccflags]+env["CCFLAGS"],LINKFLAGS=target[linkflags]+env["LINKFLAGS"])

scons

scons: Reading SConscript files ...

scons: done reading SConscript files.

scons: Building targets ...

g++ -o 1_boost.coroutine2协程实例.o -c -g3 -O0 -Wall 1_boost.coroutine2协程实例.cc

g++ -o 1_boost.coroutine2协程实例.out 1_boost.coroutine2协程实例.o -lboost_coroutine -lboost_context

g++ -o 2_boost.coroutine2_协程.o -c -g3 -O0 -Wall 2_boost.coroutine2_协程.cc

g++ -o 2_boost.coroutine2_协程.out 2_boost.coroutine2_协程.o -lboost_coroutine -lboost_context

scons: done building targets.

3.运行

gitee在线代码

协程看打印结果,意义不大;

建议gdb调试, 跟踪查看协程是如何跳转, 流程处理顺序;

这样才能掌握的更深一些, 写出有价值的协程处理.

思考

  1. 2个例子, 只做了2个协程相互交替运行
  2. 如何像python asyncio, 多个协议同时交替运行
  3. 对应系统调用阻塞API,read/select/sleep, 是否能够切换到另一个协程

相关推荐
快乐的划水a7 小时前
组合模式及优化
c++·设计模式·组合模式
星星火柴9368 小时前
关于“双指针法“的总结
数据结构·c++·笔记·学习·算法
艾莉丝努力练剑9 小时前
【洛谷刷题】用C语言和C++做一些入门题,练习洛谷IDE模式:分支机构(一)
c语言·开发语言·数据结构·c++·学习·算法
LinXunFeng10 小时前
Flutter - 详情页 TabBar 与模块联动?秒了!
前端·flutter·开源
Cx330❀11 小时前
【数据结构初阶】--排序(五):计数排序,排序算法复杂度对比和稳定性分析
c语言·数据结构·经验分享·笔记·算法·排序算法
阿巴~阿巴~11 小时前
深入解析C++ STL链表(List)模拟实现
开发语言·c++·链表·stl·list
..过云雨12 小时前
01.【数据结构-C语言】数据结构概念&算法效率(时间复杂度和空间复杂度)
c语言·数据结构·笔记·学习
旺小仔.12 小时前
双指针和codetop复习
数据结构·c++·算法
jingfeng51413 小时前
C++ STL-string类底层实现
前端·c++·算法
郝学胜-神的一滴13 小时前
基于C++的词法分析器:使用正则表达式的实现
开发语言·c++·程序人生·正则表达式·stl