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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

协程打印(1~10)

發布時間:2024/1/17 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 协程打印(1~10) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

為什么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)的全部內容,希望文章能夠幫你解決所遇到的問題。

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