日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

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

發布時間:2025/3/21 c/c++ 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c++中boost协程5种使用实例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
[java] view plaincopy
  • #include?<iostream>??
  • #include?<boost/coroutine/all.hpp>????
  • ??
  • ??
  • using?namespace?boost::coroutines;??
  • ??
  • //coroutine函數??
  • void?cooperative(coroutine<void>::push_type?&sink)??
  • {??
  • ????std::cout?<<?"Hello";??
  • ??
  • ????//之所以能夠執行是因為重載了操作符()??
  • ????//返回main()函數繼續運行??
  • ????sink();??
  • ??
  • ????std::cout?<<?"world";??
  • ??
  • ????//執行完畢,返回main繼續執行??
  • }??
  • ??
  • int?main()??
  • {??
  • ????//c++11新特性:統一初始化??
  • ????//source對象由于是pull_type類型,所以會馬上調用cooperative,?push_type類型不會立即執行??
  • ????coroutine<void>::pull_type?source{?cooperative?};??
  • ??????
  • ????std::cout?<<?",?";??
  • ??
  • ????//返回cooperative函數繼續執行??
  • ????source();??
  • ??
  • ????std::cout?<<?"!";??
  • ??
  • ????std::cout?<<?"\n";??
  • }?
  • 輸出結果

    [java] view plaincopy
  • #include?<functional>??
  • #include?<iostream>??
  • #include?<boost/coroutine/all.hpp>??
  • ??
  • ??
  • using?boost::coroutines::coroutine;??
  • ??
  • void?cooperative(coroutine<int>::push_type?&sink,?int?i)??
  • {??
  • ????int?j?=?i;??
  • ??
  • ????//調用main??
  • ????sink(++j);??
  • ??
  • ????//調用main??
  • ????sink(++j);??
  • ??
  • ????std::cout?<<?"end\n";??
  • }??
  • ??
  • int?main()??
  • {??
  • ????using?std::placeholders::_1;??
  • ??
  • ????//傳入一個參數,初始值為0??
  • ????coroutine<int>::pull_type?source{?std::bind(cooperative,?_1,?0)?};??
  • ????std::cout?<<?source.get()?<<?'\n';??
  • ??
  • ????//調用cooperative??
  • ????source();??
  • ????std::cout?<<?source.get()?<<?'\n';??
  • ??
  • ????//調用cooperative??
  • ????source();??
  • }?

  • [java] view plaincopy
  • #include?<tuple>??
  • #include?<string>??
  • #include?<iostream>??
  • #include?<boost/coroutine/all.hpp>??
  • ??
  • ??
  • using?boost::coroutines::coroutine;??
  • ??
  • void?cooperative(coroutine<std::tuple<int,?std::string>>::pull_type?&source)??
  • {??
  • ????auto?args?=?source.get();??
  • ????std::cout?<<?std::get<0>(args)?<<?"?"?<<?std::get<1>(args)?<<?'\n';??
  • ??
  • ????source();??
  • ??
  • ????args?=?source.get();??
  • ????std::cout?<<?std::get<0>(args)?<<?"?"?<<?std::get<1>(args)?<<?'\n';??
  • }??
  • ??
  • int?main()??
  • {??
  • ????coroutine<std::tuple<int,?std::string>>::push_type?sink{?cooperative?};??
  • ??
  • ????//通過tuple傳遞多個參數??
  • ????sink(std::make_tuple(0,?"aaa"));??
  • ??
  • ????//通過tuple傳遞多個參數??
  • ????sink(std::make_tuple(1,?"bbb"));??
  • ??
  • ????std::cout?<<?"end\n";??
  • }??
  • #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'; } }

    總結

    以上是生活随笔為你收集整理的c++中boost协程5种使用实例的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。