c++中boost协程5种使用实例
来源:互联网 发布:淘宝手机客服端下拉词 编辑:程序博客网 时间:2024/06/10 07:38
- #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";
- }
输出结果
- #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();
- }
- #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'; } }
阅读全文
0 0
- c++中boost协程5种使用实例
- [C++] C++中boost::any的使用
- boost.array 使用实例
- boost:exception使用实例
- boost:thread使用实例
- boost::asio 使用实例
- Boost.Asio使用实例
- boost的库中bind和function使用实例
- C/C++中使用Base64编码解码(使用boost库)
- C++--boost简单使用
- Boost.Regex库使用实例
- 如何在Open C/C++应用中使用Boost.Regex
- Ubuntu编译安装boost并在eclipse C/C++中使用/Boost安装和卸载
- boost中unorder_set使用
- C++,boost简单使用1
- C++ 调用Boost 使用正则表达式实例
- boost::threadpool线程池使用实例
- boost之asio同步io使用实例
- Oracle基础操作
- 利用jxl读取xls文件
- uTorrent设置,防止出现磁盘负荷100%
- bootstrap-table当查询后再去点击分页时,后台返回的数据是正确的,前台也显示正确了,但是当我点击分页时,列表会刷新为最初的数据,而不再是本次查询得到的数据
- 常识-Hibernate与JPA常用注解
- c++中boost协程5种使用实例
- 感悟:学编程语言就像学方言
- 在Centos7下捣鼓邮件发送软件sendmail与postfix
- 对于AOP的简单理解:AOP的含义及作用
- Apache默认虚拟主机
- 117. Populating Next Right Pointers in Each Node II
- [使用教程]xftp5中文版怎么使用?
- python 错误处理 assert
- Web 开发中的接口定义总结