boost 1.53 的 boost.coroutine协程库

评论区反馈,已失去实效性,请关注boost官方最新动态

--------------------------------------------

boost库总是会给人带来惊喜,换了1.53好久,一直没去看更新内容,今天用到原子操作了,一看Boost里面有,good!

再看有一个boost.coroutine,哈,爽!查了下用法,看来入库后比原版简化了不少,应该算是对称协程,boost特点,用起来特别简单

#include <boost/coroutine/coroutine.hpp>
#include <string>
int main(int argc, char* argv[])
{
  // 类型声明类似boost.function不过这里不是函数原型,而算是调用与返回的通讯协议
    typedef boost::coroutines::coroutine<std::string(std::string)>  myCoro_t;

  // 函数体的原型是被固定为void(T::caller_type&)类型的和作者原版不太一样
    myCoro_t coro([](myCoro_t::caller_type& caller){        
            while(caller.has_result())
            {
                if (caller.get()=="exit")
                    return;
                std::cout << caller.get() << std::endl;
                caller("ok");
            }
        },"this your arg");  // 初始化可以不传入参数,但是调用get前需要判断has_result否则触发断言
  if (coro.has_result())
        std::cout << coro.get() << std::endl;
    
    coro("give you the arg again");
    if (coro.has_result())
        std::cout << coro.get() << std::endl;

    coro("exit");
    if (!coro)  // 执行完毕或者被设置空后为false
       std::cout << "the coroutine is complete" << std::endl;

    return 0;
}

转载于:https://www.cnblogs.com/wuyaSama/archive/2013/05/28/3104114.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值