为什么80%的码农都做不了架构师?>>>   

#include <stdio.h>
#include <stdlib.h>
#include <ucontext.h>/* The three contexts:*    (1) main_context1 : The point in main to which loop will return.*    (2) main_context2 : The point in main to which control from loop will*                        flow by switching contexts.*    (3) loop_context  : The point in loop to which control from main will*                        flow by switching contexts. */
ucontext_t main_context1, main_context2, loop_context;/* The iterator return value. */
volatile int i_from_iterator;/* This is the iterator function. It is entered on the first call to* swapcontext, and loops from 0 to 9. Each value is saved in i_from_iterator,* and then swapcontext used to return to the main loop.  The main loop prints* the value and calls swapcontext to swap back into the function. When the end* of the loop is reached, the function exits, and execution switches to the* context pointed to by main_context1. */
void loop(ucontext_t *loop_context,ucontext_t *other_context,int *i_from_iterator)
{int i;for (i=0; i < 10; ++i) {/* Write the loop counter into the iterator return location. */*i_from_iterator = i;/* Save the loop context (this point in the code) into ''loop_context'',* and switch to other_context. */swapcontext(loop_context, other_context);}/* The function falls through to the calling context with an implicit* ''setcontext(&loop_context->uc_link);'' */
} int main(void)
{/* The stack for the iterator function. */char iterator_stack[SIGSTKSZ];/* Flag indicating that the iterator has completed. */volatile int iterator_finished;getcontext(&loop_context);/* Initialise the iterator context. uc_link points to main_context1, the* point to return to when the iterator finishes. */loop_context.uc_link          = &main_context1;loop_context.uc_stack.ss_sp   = iterator_stack;loop_context.uc_stack.ss_size = sizeof(iterator_stack);/* Fill in loop_context so that it makes swapcontext start loop. The* (void (*)(void)) typecast is to avoid a compiler warning but it is* not relevant to the behaviour of the function. */makecontext(&loop_context, (void (*)(void)) loop,3, &loop_context, &main_context2, &i_from_iterator);/* Clear the finished flag. */      iterator_finished = 0;/* Save the current context into main_context1. When loop is finished,* control flow will return to this point. */getcontext(&main_context1);if (!iterator_finished) {/* Set iterator_finished so that when the previous getcontext is* returned to via uc_link, the above if condition is false and the* iterator is not restarted. */iterator_finished = 1;while (1) {/* Save this point into main_context2 and switch into the iterator.* The first call will begin loop.  Subsequent calls will switch to* the swapcontext in loop. */swapcontext(&main_context2, &loop_context);printf("%d\n", i_from_iterator);}}return 0;
}

转载于:https://my.oschina.net/tsh/blog/1456963

协程打印(1~10)相关推荐

  1. Generator(生成器),入门初基,Coroutine(原生协程),登峰造极,Python3.10并发异步编程async底层实现

    普遍意义上讲,生成器是一种特殊的迭代器,它可以在执行过程中暂停并在恢复执行时保留它的状态.而协程,则可以让一个函数在执行过程中暂停并在恢复执行时保留它的状态,在Python3.10中,原生协程的实现手 ...

  2. python_21_线程+进程+协程

    python_线程_进程_协程 什么是线程? -- os能够进行运算调度的最小单位,被包含在进程之中,是一串指令的集合 -- 每个线程都是独立的,可以访问同一进程下所有的资源 什么是进程? -- 每个 ...

  3. 第 十七 章迭代器、生成器和经典协程

    当我在自己的程序中看到模式时,我认为这是一个麻烦的迹象.程序的形状应该只反映它需要解决的问题.代码中的任何其他规律性都表明,至少对我来说,我对抽象的理解还不够深--通常是我手动完成的事情,本应该通过写 ...

  4. python协程--yield和yield from

    字典为动词"to yield"给出了两个释义:产出和让步.对于 Python 生成器中的 yield 来说,这两个含义都成立.yield item 这行代码会产出一个值,提供给 n ...

  5. c++开源协程库libgo介绍及使用

    协程这个概念,最近这几年可是相当地流行了.尤其 go 语言问世之后,内置的协程特性,完全屏蔽了操作系统线程的复杂细节.甚至使 go 开发者"只知有协程,不知有线程"了.当然 C++ ...

  6. [Unity3D]-协程的介绍和使用

    本文是个人对Unity协程的一些理解和总结.Unity协程长的有点像线程,但却不是线程.因为协程仍然是在主线程中执行,且在使用时不用考虑同步与锁的问题.协程只是控制代码等到特定的时机后再执行后续步骤. ...

  7. python 协程可以嵌套协程吗_Python实战异步爬虫(协程)+分布式爬虫(多进程)

    引言:我们在写爬虫时常会遇到这样的问题,当需要爬取多个URL时,写一个普通的基于requests库的爬虫程序爬取时间会很长.因为是顺序请求网页的,而网页请求和获得响应过程比较耗费时间,程序不得不等待获 ...

  8. python异步爬虫_Python实战异步爬虫(协程)+分布式爬虫(多进程)

    转自:https://blog.csdn.net/SL_World/article/details/86633611 在讲解之前,我们先来通过一幅图看清多进程和协程的爬虫之间的原理及其区别.(图片来源 ...

  9. php swoole学习,【php】Swoole 协程学习

    第一次接触协程这个概念,是在学习Swoole时,那时看官方文档并不能完全理解协程到底是个什么东西以及该如何正确的使用它. 后来逐渐看了一些写的比较通俗的文章,加上自己的一些理解,逐步开始对协程有一些认 ...

最新文章

  1. element table多选只能选中当前页数据_element-ui里的el-table 前端分页
  2. 如何在vscode运行php代码_如何提高 PHP 代码的质量?
  3. IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python
  4. UA OPTI501 电磁波 LIH介质中的平面波1 平面波的性质
  5. 被踢出sci_心痛啊!全世界历史最长的期刊,被踢出SCI了!
  6. php重定向和伪静态,Apache301重定向和伪静态设置教程(wp程序为例)
  7. Linux系统运维成长记
  8. 搭建Spring开发环境并编写第一个Spring小程序
  9. 抖音用计算机弹曲子,抖音计算机乐谱有哪些?抖音计算机乐谱大全分享
  10. 互联网晚报 | 7月27日 星期三 | 微信安装包11年膨胀575倍;阿里申请香港纽约双重主要上市;苹果应用商店被曝大量色情应用...
  11. 模拟生成微软序列号python_【python】13位随机序列号生成工具 源码分析
  12. 计算机快捷键任务管理器,任务管理器快捷键,小编教你电脑如何打开任务管理器...
  13. ConfuserEx加密工具
  14. 不懂 CAN ?一文告诉你CAN协议!
  15. 实验十七 通信录csv文件管理
  16. IBM智慧商务 - IBM和SugarCRM携手提供全方位渠道客户体验
  17. Node - 从0基础到实战企业官网
  18. 雷军做程序员时写的博客,真心强啊。。
  19. 常用类/ID命名举例
  20. Keystore was tampered with, or password was incorrect

热门文章

  1. 无线对讲调度服务器,无线对讲系统解决方案
  2. python调用chrome插件_Python使用Chrome插件实现爬虫过程图解
  3. 录像带转存电脑的方法_误删微信记录别着急,教你几招可靠的恢复微信记录方法...
  4. thinkphp项目mysql类关系_ThinkPHP数据库与模型
  5. sounds speech_speech sounds
  6. oracle常用角色权限,Oracle角色、权限的一些常用视图 - 一夜寒江
  7. 模拟撞击_模玩资讯:EPOCH 汽车撞击测试用模拟假人与实验车辆
  8. flask框架创建数据库定义字段类型和字段常用参数
  9. spring整合问题集合1
  10. 俄罗斯方块之四 运动块的绘制实现