简而言之:

>当调用spawn()时,Boost.Asio执行一些设置工作,然后将使用strand到dispatch()内部处理程序,该处理程序使用用户提供的函数作为入口点创建协同程序.在某些条件下,可以在对spawn()的调用中调用内部处理程序,有时将其发布到io_service以进行延迟调用.

>协程暂停,直到操作完成并调用完成处理程序,io_service被销毁,或者Boost.Asio检测到协程已被挂起而无法恢复它,此时Boost.Asio将破坏协同程序.

如上所述,当调用spawn()时,然后使用strand来调度()内部处理程序,该处理程序使用用户提供的函数作为入口点创建协同程序.当yield_context对象作为处理程序传递给异步操作时,Boost.Asio将在使用完成处理程序启动异步操作后立即生成,该处理程序将复制结果并恢复协程.前面提到的链是由coroutine所有,用于保证在恢复之前产生的产量.让我们考虑一个简单的例子demonstrating spawn():

#include

#include

#include

boost::asio::io_service io_service;

void other_work()

{

std::cout << "Other work" << std::endl;

}

void my_work(boost::asio::yield_context yield_context)

{

// Add more work to the io_service.

io_service.post(&other_work);

// Wait on a timer within the coroutine.

boost::asio::deadline_timer timer(io_service);

timer.expires_from_now(boost::posix_time::seconds(1));

std::cout << "Start wait" << std::endl;

timer.async_wait(yield_context);

std::cout << "Woke up" << std::endl;

}

int main ()

{

boost::asio::spawn(io_service,&my_work);

io_service.run();

}

以上示例输出:

Start wait

Other work

Woke up

这是尝试说明示例的执行.路径|表示活动堆栈,:表示挂起的堆栈,箭头用于表示控制转移:

boost::asio::io_service io_service;

boost::asio::spawn(io_service,&my_work);

`-- dispatch a coroutine creator

into the io_service.

io_service.run();

|-- invoke the coroutine creator

| handler.

| |-- create and jump into

| | into coroutine ----> my_work()

: : |-- post &other_work onto

: : | the io_service

: : |-- create timer

: : |-- set timer expiration

: : |-- cout << "Start wait" << endl;

: : |-- timer.async_wait(yield)

: : | |-- create error_code on stack

: : | |-- initiate async_wait operation,: : | | passing in completion handler that

: : | | will resume the coroutine

| `-- return

|-- io_service has work (the : :

| &other_work and async_wait) : :

|-- invoke other_work() : :

| `-- cout << "Other work" : :

| << endl; : :

|-- io_service still has work : :

| (the async_wait operation) : :

| ...async wait completes... : :

|-- invoke completion handler : :

| |-- copies error_code : :

| | provided by service : :

| | into the one on the : :

| | coroutine stack : :

| |-- resume ----> | `-- return error code

: : |-- cout << "Woke up." << endl;

: : |-- exiting my_work block,timer is

: : | destroyed.

| `-- return

`-- no outstanding work in

io_service,return.

c语言spawn函数什么作用,c – boost :: asio :: spawn有什么作用?相关推荐

  1. c语言 spawn函数,我在electron程序中spawn了一个C语言程序作为子进程,我该如何与这个子进程通信?...

    C语言的代码为: #include int main(){ int i = 0; scanf`("%d", &i);` printf`("%d\n",  ...

  2. c语言get()的作用,c语言get函数的用法有哪些

    C语言中gets从标准输入设备读字符串函数,可以无限读取,不会判断上限,今天学习啦小编在这里为大家介绍C语言get函数的用法,欢迎大家阅读! c语言get函数的用法:int getc(FILE *st ...

  3. Boost::asio概览

    Boost::asio核心的概念和功能 asio的最核心的功能是用于异步的IO通信, 比如通过文件 网络或者控制台等. asio提供了一系列的工具来处理这种长时间的IO操作, 而且执行这些操作不需要依 ...

  4. boost::asio译文

    boost::asio译文 Christopher Kohlhoff Copyright © 2003-2012 Christopher M. Kohlhoff 以Boost1.0的软件授权进行发布( ...

  5. Boost.Asio技术文档汇总

    Christopher Kohlhoff Copyright © 2003-2012 Christopher M. Kohlhoff 以Boost1.0的软件授权进行发布(见附带的LICENSE_1_ ...

  6. VScode CMake 编写 Boost Asio Chat程序----记录6

    目录 一 前言 二  参考 三 程序 四 解读 五  注意事项 一 前言 实现了如下功能: 1 留言室(原功能). 2 两个client可以相互交谈 但是需要离开room, 要想留言,需要重新进入, ...

  7. boost::asio中文文档

    Christopher Kohlhoff Copyright © 2003-2012 Christopher M. Kohlhoff 以Boost1.0的软件授权进行发布(见附带的LICENSE_1_ ...

  8. Boost.Asio技术文档

    文章来源:http://blog.csdn.net/henreash/article/details/7469707 Boost.Asio是一个跨平台的网络及底层IO的C++编程库,它使用现代C++手 ...

  9. Boost.Asio 技术文档

    Christopher Kohlhoff Copyright © 2003-2012 Christopher M. Kohlhoff 以Boost1.0的软件授权进行发布(见附带的LICENSE_1_ ...

最新文章

  1. remote 移除_remote模块的使用(四)
  2. eclipse java 7_在Eclipse中编程Java 7
  3. C#.net Winform获取文件路径
  4. 每个Web开发者都应该知道的SOLID原则
  5. httpclient4 中文版帮助文档,最新官方版翻译版(第一章 上)
  6. 管理类软件设计“渔”之演化
  7. jQuery 属性,css,文档操作
  8. TabBarController创建及使用方法简介
  9. 【课程】MIT最新深度学习课程集
  10. MyEclipse 10, 2013, 2014 破解、注册码
  11. leetcode题库:2.两数相加
  12. 程序员值得思考的问题
  13. ROS入门 小乌龟跟随示例
  14. 交叉编译中libtool相关的问题
  15. 模拟人生4极乐净土mod_如何在《模拟人生4》中下载Mod
  16. 电工和电气工程师的区别
  17. 计算机专业为职业环境分析,2021年计算机职业环境分析报告|计算机职业环境分析-得范文网...
  18. 注意收藏 !2022年下半年软考时间都在这
  19. linux ip_conntrack_max,解?Linux NAT ip_conntrack: table full的方法
  20. 实例011阳阳买苹果

热门文章

  1. VSCode官方插件-Egret Coder 解决了文件代码编辑、调用命令、断点调试等问题
  2. B端金融产品笔记二(产品岗位的思考)
  3. DBeaver安装与使用教程(超详细安装与使用教程),好用免费的数据库管理工具
  4. CtsTelephonyTestCases
  5. 2022湖南最新八大员之(安全员)模拟考试试题及答案
  6. ElasticSearch的multiMatch方法,输入字段过长时,报如下错误,怎么解决?
  7. centos8时间比实际时间快8小时
  8. MarkDown learn的Typora工具简介使用说明
  9. 专业才是人生逆袭的武器
  10. 计算机基础算销售排名公式,办公人员必备12个函数公式(统计排名等),提高工作效率...