#include"uthread.h"structc" />

亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

C協程使用舉例

系統 2258 0

C協程使用舉例 - sniperHW - 博客園

C協程使用舉例

本篇使用上一篇提供的接口,實現一個簡單的協程調度框架.

基本思想是,創建一個調度器,用于將處于活動狀態的協程調度運行,調度器維護著一個actived列表,

調用spawn創建協程時,將新建立的協程添加到活動列表中。

調用schedule將啟動調度器主循環.

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;  
            
              //
            
            
              處于激活態的coro
            
            
              struct
            
             coro *
            
              self;
};


            
            
              struct
            
             scheduler *
            
              scheduler_create();

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

            
            
              //
            
            
              調度coro運行
            
            
              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;
    
            
            
              //
            
            
              添加到激活隊列中
            
            
    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協程使用舉例


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 日本午夜色 | 一区二区视频在线观看免费的 | 色片网站在线观看 | 91成人爽a毛片一区二区 | 久久综合九色综合欧美狠狠 | 在线观看a视频 | 久久久久久久久久久9精品视频 | 毛茸茸free牲交 | 国产视频在线观看福利 | 99爱视频精品免视看 | 久久97久久97精品免视看清纯 | 精品综合久久久久久蜜月 | 日韩欧美色 | 亚欧在线免费观看 | 四虎www.| 99热在线精品观看 | 咪咪色综合 | 99免费视频观看 | 成人免费在线视频 | 狠狠狠狼鲁欧美综合网免费 | 日本一级毛片在线观看 | 在线观看欧美亚洲日本专区 | 国产在线精品福利91香蕉 | 欧美日韩综合精品一区二区三区 | 亚洲免费视频网 | 波多野结衣xxxx性精品 | 国产成人麻豆精品video | 四虎澳门永久8848在线影院 | 亚洲免费色视频 | 香蕉一区二区三区观 | 日本一区二区三区四区在线观看 | 亚洲欧洲免费视频 | 国产乱在线观看视频 | 四虎精品影院2022 | 四虎最新永久在线精品免费 | 九九热视频在线播放 | 国产福利小视频在线观看 | 久久国产精品亚洲综合 | 99精品高清视频一区二区 | 一级免费黄色毛片 | 一区二区视频在线观看 |