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

来源:互联网 发布:淘宝手机客服端下拉词 编辑:程序博客网 时间:2024/06/10 07:38
[java] view plain copy
  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";  

输出结果

[java] view plain copy
  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();  


[java] view plain copy
  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';      }  } 


原创粉丝点击