c++中boost协程5种使用实例

  1. #include <iostream>  
  2. #include <boost/coroutine/all.hpp>    
  3.   
  4.   
  5. using namespace boost::coroutines;  
  6.   
  7. //coroutine函数  
  8. void cooperative(coroutine<void>::push_type &sink)  
  9. {  
  10.     std::cout << "Hello";  
  11.   
  12.     //之所以能够执行是因为重载了操作符()  
  13.     //返回main()函数继续运行  
  14.     sink();  
  15.   
  16.     std::cout << "world";  
  17.   
  18.     //执行完毕,返回main继续执行  
  19. }  
  20.   
  21. int main()  
  22. {  
  23.     //c++11新特性:统一初始化  
  24.     //source对象由于是pull_type类型,所以会马上调用cooperative, push_type类型不会立即执行  
  25.     coroutine<void>::pull_type source{ cooperative };  
  26.       
  27.     std::cout << ", ";  
  28.   
  29.     //返回cooperative函数继续执行  
  30.     source();  
  31.   
  32.     std::cout << "!";  
  33.   
  34.     std::cout << "\n";  

输出结果

  1. #include <functional>  
  2. #include <iostream>  
  3. #include <boost/coroutine/all.hpp>  
  4.   
  5.   
  6. using boost::coroutines::coroutine;  
  7.   
  8. void cooperative(coroutine<int>::push_type &sink, int i)  
  9. {  
  10.     int j = i;  
  11.   
  12.     //调用main  
  13.     sink(++j);  
  14.   
  15.     //调用main  
  16.     sink(++j);  
  17.   
  18.     std::cout << "end\n";  
  19. }  
  20.   
  21. int main()  
  22. {  
  23.     using std::placeholders::_1;  
  24.   
  25.     //传入一个参数,初始值为0  
  26.     coroutine<int>::pull_type source{ std::bind(cooperative, _1, 0) };  
  27.     std::cout << source.get() << '\n';  
  28.   
  29.     //调用cooperative  
  30.     source();  
  31.     std::cout << source.get() << '\n';  
  32.   
  33.     //调用cooperative  
  34.     source();  


  1. #include <tuple>  
  2. #include <string>  
  3. #include <iostream>  
  4. #include <boost/coroutine/all.hpp>  
  5.   
  6.   
  7. using boost::coroutines::coroutine;  
  8.   
  9. void cooperative(coroutine<std::tuple<int, std::string>>::pull_type &source)  
  10. {  
  11.     auto args = source.get();  
  12.     std::cout << std::get<0>(args) << " " << std::get<1>(args) << '\n';  
  13.   
  14.     source();  
  15.   
  16.     args = source.get();  
  17.     std::cout << std::get<0>(args) << " " << std::get<1>(args) << '\n';  
  18. }  
  19.   
  20. int main()  
  21. {  
  22.     coroutine<std::tuple<int, std::string>>::push_type sink{ cooperative };  
  23.   
  24.     //通过tuple传递多个参数  
  25.     sink(std::make_tuple(0"aaa"));  
  26.   
  27.     //通过tuple传递多个参数  
  28.     sink(std::make_tuple(1"bbb"));  
  29.   
  30.     std::cout << "end\n";  
  31. }  
#include <iostream>
#include <cstdlib>


#include <boost/coroutine2/all.hpp>


int main()
{
int i = 0;
boost::coroutines2::coroutine< void >::push_type sink(
[&](boost::coroutines2::coroutine< void >::pull_type & source) {
std::cout << "inside coroutine-fn" << std::endl;
});
sink();


std::cout << "\nDone" << std::endl;


return EXIT_SUCCESS;

}
#include <stdexcept>  
#include <iostream>  
#include <boost/coroutine/all.hpp>  
  
  
using boost::coroutines::coroutine;  
  
void cooperative(coroutine<void>::push_type &sink)  
{  
    //返回main  
    sink();  
    throw std::runtime_error("error");  
}  
  
int main()  
{  
    coroutine<void>::pull_type source{ cooperative };  
    try  
    {  
        //调用cooperative  
        source();  
  
        //捕获抛出的异常std::runtime_error  
    }  
    catch (const std::runtime_error &e)  
    {  
        std::cerr << e.what() << '\n';  
    }  
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值