zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    月薪3万+的大数据人都在疯学Flink,为什么?
    Flink应用场景
    Flink生态与未来
    flink学习笔记-split & select(拆分流)
    .NET MVC5简介(四)Filter和AuthorizeAttribute权限验证
    .NET MVC5简介(三)Result
    .NET MVC5简介(二)
    VUE基础实用技巧
    .NET MVC5简介(一)
    WebApi简介
  • 原文地址:https://www.cnblogs.com/wuyaSama/p/3104114.html
Copyright © 2011-2022 走看看