当前位置: 首页 > news >正文

win7做网站服务器卡免费公司网站制作

win7做网站服务器卡,免费公司网站制作,做公司网页步骤,wordpress仿站方法C20协程示例 认识协程 在C中#xff0c;协程就是一个可以暂停和恢复的函数。 包含co_wait、co_yield、co_return关键字的都可以叫协程。 看一个例子#xff1a; MyCoroGeneratorint testFunc(int n) {std::cout Begin testFunc s…C20协程示例 认识协程 在C中协程就是一个可以暂停和恢复的函数。 包含co_wait、co_yield、co_return关键字的都可以叫协程。 看一个例子 MyCoroGeneratorint testFunc(int n) {std::cout Begin testFunc std::endl;for (int i 0; i n; i) {std::cout TestFunc before yield i std::endl;co_yield i;std::cout TestFunc after yield i std::endl;}std::cout End testFunc std::endl; }int main() {int inp 10;std::cout Before testFunc std::endl;MyCoroGeneratorint gen testFunc(inp);std::cout After testFunc std::endl;for (int i 0; i inp; i) {std::cout Cur input: i std::endl;std::cout Output value: gen.next() std::endl;std::cout After input: i std::endl;} } 上面这段代码的执行结果是 Before testFunc After testFunc Cur input: 0 Output value: Begin testFunc TestFunc before yield 0 0 After input: 0 Cur input: 1 Output value: TestFunc after yield 0 TestFunc before yield 1 1 After input: 1 Cur input: 2 Output value: TestFunc after yield 1 TestFunc before yield 2 2 After input: 2 Cur input: 3 Output value: TestFunc after yield 2 TestFunc before yield 3 3 After input: 3 Cur input: 4 Output value: TestFunc after yield 3 TestFunc before yield 4 4 After input: 4 Cur input: 5 Output value: TestFunc after yield 4 TestFunc before yield 5 5 After input: 5... 调用时发现函数并没有一开始就执行而是等到next时才开始执行。执行到co_yield就会自动暂停下次next才会继续执行 另外函数本身并没有返回什么但是这里可以使用MyCoroGeneratorint接收。函数的控制权也转移到了MyCoroGeneratorint需要执行时就调用next。 函数在暂停后执行依然可以继续上次的状态这是因为协程在挂起期间其上下文全部都被保留下次执行时恢复。 协程句柄 std::coroutine_handle类模板是实现协程的最底层的工具负责存储协程的句柄。他可以特化为std::coroutine_handlePromise或者std::coroutine_handlevoid。 这里面的Promise是实现协程必要的Promise类而且其名字必须是promise_type。 协程会暂停其上下文也会保留std::coroutine_handle就是保存和管理协程的地方。 std::coroutine_handle中方法不多但是每个都很重要其中 done方法可以查询协程是否已经结束resume恢复一个协程的执行destroy销毁一个协程 std::coroutine_handle是一个很底层的东西没有RAII包裹就像一个裸指针那样需要手动创建、手动销毁。 所以需要一个包裹类负责处理协程句柄的初始化和销毁也就是Generator Generator C的协程要求Generator必须有promise_type这个类名字也必须这个不能是其他的名字直接定义在Generator中最方便。 template typename T struct MyCoroGenerator {struct promise_type {// todo}; }; promise_type中有一些固定接口这些接口如果不实现那么协程是不完整的。 initial_suspend协程是否在初始化结束后挂起返回std::suspend_always、std::suspend_never这是标准库里面已经定义好的类型前者表示总是挂起后者表示从不挂起final_suspend协程最后一次执行是否挂起返回值和上面函数相同。由于final_suspend是收尾阶段的工作所以必须是noexceptunhandled_exception处理协程中未被处理的异常get_return_object返回一个Generator对象yield_value处理协程返回值就是co_yield传递过来的值return_void处理协程结束后的返回值和return_value同时只能存在一个如果没有返回值就用return_voidreturn_value处理协程结束后返回值和return_void同时只能存在一个如果有返回值就用return_value 解释一下yield_value函数co_yield实际是一个语法糖相当于co_wait promise.yield_value(i)只有实现了该函数Genreator才能接收co_yield的返回值 函数的返回值需要回答协程要不要挂起也就是std::suspend_always或者std::suspend_never因为需要yield后挂起所以返回std::suspend_always。 为了接收返回值需要用转发和std::optional接收并存储返回值。 get_return_object负责创建一个Generator一般来说使用std::coroutine_handlepromise_type::from_promise接口从一个Promise创建句柄然后使用这个句柄创建Generator。 为了配合创建函数MyGenerator需要实现一个接收句柄的构造函数。也就是MyCoroGenerator(std::coroutine_handlepromise_type h) #include coroutine #include iostream #include optionaltemplate typename T struct MyCoroGenerator {/*** brief C协程要求Generator必须有promise_type这个类型名字也必须是promise_type* 最方便的方法就是定义在Generator类内部**/struct promise_type {/*** brief 存放协程返回值* 由Generator从获取并返回*/std::optionalT opt;/*** brief 协程是否创建时就被挂起函数名字也必须是initial_suspend* std::suspend_always、std::suspend_never是标准库里面已经定义好的类型前者表示总是挂起后者表示从不挂起** return std::suspend_always 协程创建即挂起*/std::suspend_always initial_suspend() const{return {};}/*** brief 协程最后一次执行是否挂起函数名字也必须是final_suspend* 由于final_suspend是收尾阶段的工作所以必须是noexcept** return std::suspend_always 协程最后一次执行也被挂起*/std::suspend_always final_suspend() const noexcept{return {};}/*** brief 处理协程中未捕获异常函数名字必须是unhandled_exception**/void unhandled_exception(){std::exit(EXIT_FAILURE);}/*** brief 获取一个Generator对象该对象从promise_type构造** return MyCoroGenerator*/MyCoroGenerator get_return_object(){return MyCoroGenerator { std::coroutine_handlepromise_type::from_promise(*this) };}/*** brief 定制yield_value接口接收co_yield返回的值** tparam Arg 值的类型* param arg co_yield返回值* return std::suspend_always 执行完后继续挂起*/template typename Argstd::suspend_always yield_value(Arg arg){opt.emplace(std::forwardArg(arg));return {};}/*** brief 当协程结束co_return且没有返回值时调用该函数* 还有一个return_value(expr)函数负责处理协程结束且有返回值的情况*/void return_void(){}};/*** brief 协程句柄存储了协程上下文一个非常底层的东西没有RAII* 用MyCoroGenerator包裹*/std::coroutine_handlepromise_type handle;/*** brief 默认构造函数**/MyCoroGenerator() default;/*** brief 通过一个handle构造一个Generator** param h 从promise_type构造出来的协程句柄*/MyCoroGenerator(std::coroutine_handlepromise_type h): handle(h){}/*** brief 移动构造函数** param other 其他Generator对象*/MyCoroGenerator(MyCoroGenerator other){if (handle) {handle.destroy();}handle other.handle;other.handle nullptr;}/*** brief 析构函数**/~MyCoroGenerator(){if (handle) {handle.destroy();}}/*** brief 移动赋值函数** param other 其他Generator对象* return MyCoroGenerator 当前镀锡*/MyCoroGenerator operator(MyCoroGenerator other){if (handle) {handle.destroy();}handle other.handle;other.handle nullptr;return *this;}/*** brief 继续执行协程并返回执行结果** return T 返回的值*/T next(){handle.resume();if (handle.done()) {// throw geneator_done(Generator done);throw Generator Error;}return *(handle.promise().opt);}private:MyCoroGenerator(const MyCoroGenerator) delete;MyCoroGenerator operator(const MyCoroGenerator) delete; }; 源码 #include coroutine #include iostream #include optionaltemplate typename T struct MyCoroGenerator {/*** brief C协程要求Generator必须有promise_type这个类型名字也必须是promise_type* 最方便的方法就是定义在Generator类内部**/struct promise_type {/*** brief 存放协程返回值* 由Generator从获取并返回*/std::optionalT opt;/*** brief 协程是否创建时就被挂起函数名字也必须是initial_suspend* std::suspend_always、std::suspend_never是标准库里面已经定义好的类型前者表示总是挂起后者表示从不挂起** return std::suspend_always 协程创建即挂起*/std::suspend_always initial_suspend() const{return {};}/*** brief 协程最后一次执行是否挂起函数名字也必须是final_suspend* 由于final_suspend是收尾阶段的工作所以必须是noexcept** return std::suspend_always 协程最后一次执行也被挂起*/std::suspend_always final_suspend() const noexcept{return {};}/*** brief 处理协程中未捕获异常函数名字必须是unhandled_exception**/void unhandled_exception(){std::exit(EXIT_FAILURE);}/*** brief 获取一个Generator对象该对象从promise_type构造** return MyCoroGenerator*/MyCoroGenerator get_return_object(){return MyCoroGenerator { std::coroutine_handlepromise_type::from_promise(*this) };}/*** brief 定制yield_value接口接收co_yield返回的值** tparam Arg 值的类型* param arg co_yield返回值* return std::suspend_always 执行完后继续挂起*/template typename Argstd::suspend_always yield_value(Arg arg){opt.emplace(std::forwardArg(arg));return {};}/*** brief 当协程结束co_return且没有返回值时调用该函数* 还有一个return_value(expr)函数负责处理协程结束且有返回值的情况*/void return_void(){}};/*** brief 协程句柄存储了协程上下文一个非常底层的东西没有RAII* 用MyCoroGenerator包裹*/std::coroutine_handlepromise_type handle;/*** brief 默认构造函数**/MyCoroGenerator() default;/*** brief 通过一个handle构造一个Generator** param h 从promise_type构造出来的协程句柄*/MyCoroGenerator(std::coroutine_handlepromise_type h): handle(h){}/*** brief 移动构造函数** param other 其他Generator对象*/MyCoroGenerator(MyCoroGenerator other){if (handle) {handle.destroy();}handle other.handle;other.handle nullptr;}/*** brief 析构函数**/~MyCoroGenerator(){if (handle) {handle.destroy();}}/*** brief 移动赋值函数** param other 其他Generator对象* return MyCoroGenerator 当前镀锡*/MyCoroGenerator operator(MyCoroGenerator other){if (handle) {handle.destroy();}handle other.handle;other.handle nullptr;return *this;}/*** brief 继续执行协程并返回执行结果** return T 返回的值*/T next(){handle.resume();if (handle.done()) {// throw geneator_done(Generator done);throw Generator Error;}return *(handle.promise().opt);}private:MyCoroGenerator(const MyCoroGenerator) delete;MyCoroGenerator operator(const MyCoroGenerator) delete; };MyCoroGeneratorint testFunc(int n) {std::cout Begin testFunc std::endl;for (int i 0; i n; i) {std::cout TestFunc before yield i std::endl;co_yield i;std::cout TestFunc after yield i std::endl;}std::cout End testFunc std::endl; }int main() {int inp 10;std::cout Before testFunc std::endl;MyCoroGeneratorint gen testFunc(inp);std::cout After testFunc std::endl;for (int i 0; i inp; i) {std::cout Cur input: i std::endl;std::cout Output value: gen.next() std::endl;std::cout After input: i std::endl;} } 参考C coroutine generator 实现笔记
http://www.hkea.cn/news/14379260/

相关文章:

  • 房产网站推广网站左悬浮代码
  • 杭州行业网站建设公司wordpress怎么新建模块
  • 机械设备公司网站制作新开传奇手游新服网
  • 哪个网站做兼职如何查看一个网站是不是用h5做的
  • 成都网站开发培训多少钱wordpress发邮件更新
  • 建设网站网桂林 网
  • 小企业公司网站怎么建学习网站导航
  • 网站建设与运营就业传统企业如何做好网络推广
  • 织梦网站去除技术支持怎么了解百度蜘蛛到哪个网站
  • wordpress静态化占内存么做百度手机网站优化快
  • 厦门建设管理局网站首页国内的电商平台
  • 行业查询网站网站建设需求建议书
  • 在线游戏网站完整网站开发视频教程
  • 静态宠物网站设计论文青岛网站制作服务商
  • 公众号建网站家具网站开发
  • 做网站市场价金华网站建设电话
  • 百度站长平台账号购买社交app定制
  • 高端的丹阳网站建设wordpress评论添加验证
  • 湖北网站建设哪里有福州网络营销推广公司
  • 学网站开发需要会什么app免费下载入口
  • 网站逻辑结构优化国外的建筑设计案例网站
  • 潍坊网站建设app个人简历模板大全
  • 网站流量超限html怎么写
  • 汕头做网站清晰化网站
  • 网站加载慢菜单制作软件app
  • 做网站听的纯音乐做企业网站前期需要准备什么资料
  • 手机网站wap软件开发去哪里学
  • 昆山市建设监察大队网站成立公司协议
  • 衡水建设网站公司专业做网站设计的公司
  • SEO案例网站建设价格无锡网站改版多少钱