#include"uthread.h"structc" />

欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

C協(xié)程使用舉例

系統(tǒng) 2364 0

C協(xié)程使用舉例 - sniperHW - 博客園

C協(xié)程使用舉例

本篇使用上一篇提供的接口,實(shí)現(xiàn)一個(gè)簡(jiǎn)單的協(xié)程調(diào)度框架.

基本思想是,創(chuàng)建一個(gè)調(diào)度器,用于將處于活動(dòng)狀態(tài)的協(xié)程調(diào)度運(yùn)行,調(diào)度器維護(hù)著一個(gè)actived列表,

調(diào)用spawn創(chuàng)建協(xié)程時(shí),將新建立的協(xié)程添加到活動(dòng)列表中。

調(diào)用schedule將啟動(dòng)調(diào)度器主循環(huán).

coro.h

            
              #ifndef _CORO_H

            
            
              #define
            
             _CORO_H
            
              
#include 
            
            <stdint.h>
            
              
#include 
            
            
              "
            
            
              uthread.h
            
            
              "
            
            
              struct
            
            
               coro
{
    
            
            
              struct
            
             coro *
            
              next;
    uthread_t ut;
    uint32_t id;
    start_fun st_fun;
    uint32_t is_end;
};


            
            
              struct
            
            
               testarg
{
    
            
            
              struct
            
             coro *
            
              sche;
    
            
            
              struct
            
             coro *
            
              co;
};


            
            
              void
            
            * 
            
              yield
            
            (
            
              struct
            
             coro*,
            
              struct
            
             coro*,
            
              void
            
             *
            
              );

            
            
              void
            
            * resume(
            
              struct
            
             coro*,
            
              struct
            
             coro*,
            
              void
            
             *
            
              );


            
            
              struct
            
            
               scheduler
{
    
            
            
              struct
            
             coro *active;  
            
              //
            
            
              處于激活態(tài)的coro
            
            
              struct
            
             coro *
            
              self;
};


            
            
              struct
            
             scheduler *
            
              scheduler_create();

            
            
              //
            
            
              生成一個(gè)coro運(yùn)行start_run
            
            
              void
            
             spawn(
            
              struct
            
             scheduler*,
            
              void
            
             *
            
              stack,uint32_t stack_size,start_fun);

            
            
              //
            
            
              調(diào)度coro運(yùn)行
            
            
              void
            
             schedule(
            
              struct
            
             scheduler*
            
              );


            
            
              #endif
            
          

coro.c

            #include 
            
              "
            
            
              coro.h
            
            
              "
            
            
              
#include 
            
            <stdlib.h>
            
              
#include 
            
            <time.h>


            
              void
            
            * 
            
              yield
            
            (
            
              struct
            
             coro *
            
              from
            
            ,
            
              struct
            
             coro *to,
            
              void
            
             *
            
              arg)
{
    
            
            
              return
            
             uthread_swtch(
            
              from
            
            ->ut,to->
            
              ut,arg);
}


            
            
              void
            
            * resume(
            
              struct
            
             coro *
            
              from
            
            ,
            
              struct
            
             coro *to,
            
              void
            
             *
            
              arg)
{
    
            
            
              return
            
             uthread_swtch(
            
              from
            
            ->ut,to->
            
              ut,arg);
}


            
            
              static
            
             uint32_t g_index = 
            
              0
            
            
              ;


            
            
              static
            
            
              void
            
            * coro_start_fun(
            
              void
            
             *
            
              arg)
{
    
            
            
              struct
            
             testarg *_arg = (
            
              struct
            
             testarg *
            
              )arg;
    
            
            
              void
            
             *ret = _arg->co->
            
              st_fun(_arg);
    _arg
            
            ->co->is_end = 
            
              1
            
            
              ;
    
            
            
              return
            
            
               ret;
}


            
            
              void
            
             spawn(
            
              struct
            
             scheduler *sche,
            
              void
            
             *
            
              stack,uint32_t stack_size,start_fun st_fun)
{
    uthread_t ut 
            
            =
            
               uthread_create(stack,stack_size);
    
            
            
              struct
            
             coro *co = (
            
              struct
            
             coro*)malloc(
            
              sizeof
            
            (*
            
              co));
    co
            
            ->ut =
            
               ut;
    co
            
            ->st_fun =
            
               st_fun;
    co
            
            ->id = ++
            
              g_index;
    
            
            
              //
            
            
              添加到激活隊(duì)列中
            
            
    co->next = sche->
            
              active;
    co
            
            ->is_end = 
            
              0
            
            
              ;
    sche
            
            ->active =
            
               co;
    uthread_make(co
            
            ->ut,sche->self->
            
              ut,coro_start_fun);
}


            
            
              struct
            
             scheduler *
            
              scheduler_create()
{
    
            
            
              struct
            
             scheduler *sche = (
            
              struct
            
             scheduler *)malloc(
            
              sizeof
            
            (*
            
              sche));
    sche
            
            ->active = 
            
              0
            
            
              ;
    sche
            
            ->self = (
            
              struct
            
             coro*)malloc(
            
              sizeof
            
            (*sche->
            
              self));
    sche
            
            ->self->ut = uthread_create(
            
              0
            
            ,
            
              0
            
            
              );
    
            
            
              return
            
            
               sche;
}


            
            
              void
            
             schedule(
            
              struct
            
             scheduler *
            
              sche)
{
    
            
            
              while
            
            (
            
              1
            
            
              )
    {
        
            
            
              if
            
            (sche->
            
              active)
        {
            
            
            
              struct
            
             coro *cur = sche->
            
              active;
            sche
            
            ->active = 
            
              0
            
            
              ;
            
            
            
              while
            
            
              (cur)
            {
                
            
            
              struct
            
             testarg arg = {sche->
            
              self,cur};
                resume(sche
            
            ->self,cur,&
            
              arg);
                
            
            
              struct
            
             coro *tmp = cur->
            
              next;
                
            
            
              if
            
            (!cur->
            
              is_end)
                {
                    cur
            
            ->next = sche->
            
              active;
                    sche
            
            ->active =
            
               cur;
                    cur 
            
            =
            
               tmp;
                }
                
            
            
              else
            
            
              
                {
                    uthread_destroy(
            
            &(cur->
            
              ut));
                    free(cur);
                }
                cur 
            
            =
            
               tmp;
            }
        }
        
            
            
              else
            
            
              break
            
            
              ;
    }

}
            
          

test.c

            #include <stdio.h>
            
              
#include 
            
            <stdlib.h>
            
              
#include 
            
            <stdint.h>
            
              
#include 
            
            
              "
            
            
              uthread.h
            
            
              "
            
            
              
#include 
            
            
              "
            
            
              coro.h
            
            
              "
            
            
              void
            
            * fun(
            
              void
            
             *
            
              arg)
{
    
            
            
              struct
            
             testarg *_arg = (
            
              struct
            
             testarg *
            
              )arg;
    
            
            
              int
            
             i = 
            
              0
            
            
              ;
    
            
            
              while
            
            (i<
            
              10
            
            
              )
    {
       printf(
            
            
              "
            
            
              %d\n
            
            
              "
            
            ,_arg->co->
            
              id);
       
            
            
              yield
            
            (_arg->co,_arg->sche,
            
              0
            
            
              );
       
            
            ++
            
              i;
    }
    
            
            
              return
            
            
              0
            
            
              ;
}


            
            
              int
            
            
               main()
{
    
            
            
              struct
            
             scheduler *sche =
            
               scheduler_create();
    spawn(sche,malloc(
            
            
              4096
            
            ),
            
              4096
            
            
              ,fun);
    spawn(sche,malloc(
            
            
              4096
            
            ),
            
              4096
            
            
              ,fun);
    spawn(sche,malloc(
            
            
              4096
            
            ),
            
              4096
            
            
              ,fun);
    spawn(sche,malloc(
            
            
              4096
            
            ),
            
              4096
            
            
              ,fun);
    schedule(sche);
    
            
            
              return
            
            
              0
            
            
              ;
}
            
          

?

C協(xié)程使用舉例


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦?。。?/p>

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 在线观看视频一区二区 | 久久精品这里是免费国产 | 精品久久久一二三区 | 男女同床爽爽视频免费 | 久久精品视频大全 | 久久福利青草精品免费 | 羞羞视频免费网站在线看 | 国产精品久久久久久久久久久久冷 | 国产色 | 久草中文字 | 九九综合九九 | 69久久夜色精品国产69 | 久久亚洲一级毛片 | 久久久九九精品国产毛片A片 | 久草热在线视频 | 欧美一级黄色网 | 色综合天天娱乐综合网 | av免费不卡国产观看 | 成人淫片免费视频95视频 | 亚洲一区二区三 | 国产精品久久久久无毒 | 天天爽夜夜爽人人爽 | 999jjj在线播放 | 欧美精品一区二区三区免费播放 | 久久精品这里是免费国产 | 免费观看性欧美一级 | 日韩版码免费福利视频 | 欧美精品3atv一区二区三区 | 污污网站国产精品白丝袜 | 亚洲精品人人 | 久久久久网站 | 激情宗合网 | 国产在线精品一区二区三区 | 欧美日韩精品在线观看 | 免费人成网站线观看合集 | 国产精品一区欧美激情 | 亚洲欧美日韩中文综合v日本 | 日本精高清区一 | 波多野结衣在线网址 | 波多野一区 | 久久综合热 |