C協程使用舉例

2021-09-08 20:48:22 字數 1614 閱讀 6811

c協程使用舉例 - sniperhw -

基本思想是,建立乙個排程器,用於將處於活動狀態的協程排程執行,排程器維護著乙個actived列表,

呼叫spawn建立協程時,將新建立的協程新增到活動列表中。

呼叫schedule將啟動排程器主迴圈.

coro.h

#ifndef _coro_h

#define _coro_h#include

#include

"uthread.h

"struct

coro

;struct

testarg

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

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

struct

scheduler

;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

#include

void* yield(struct coro *from,struct coro *to,void *arg)

void* resume(struct coro *from,struct coro *to,void *arg)

static uint32_t g_index = 0

;static

void* coro_start_fun(void *arg)

void spawn(struct scheduler *sche,void *stack,uint32_t stack_size,start_fun st_fun)

struct scheduler *scheduler_create()

void schedule(struct scheduler *sche)

; resume(sche->self,cur,&arg);

struct coro *tmp = cur->next;

if(!cur->is_end)

else

cur =tmp;}}

else

break

; }

}

test.c

#include #include 

#include

#include

"uthread.h

"#include

"coro.h

"void* fun(void *arg)

return0;

}int

main()

關於python協程與非同步舉例

寫了幾個介面,也就接觸到了python的非同步。python的協程使用非同步實現的 說法不知道準不準確 所以直接使用協程來做,看的很多例子也是這麼幹的 先看 import json import tornado import tornado.web from tornado import gen f...

Python協程理解 基於爬蟲舉例

當前 在工作當中沒有太大的含義,但是對於大家理解協程的基礎概念是相當有好處的 協程最直接的可以理解為程式當中乙個沒有返回的功能塊兒 我們之前有學過多執行緒,所謂的多執行緒不論是非同步併發,還是併發強調的時候將功能放到不同的執行緒上分別執行的過程 但是協程不是這樣的,協程強調的是在同乙個執行緒上進行執...

python協程使用 協程的案例

概念 使用者層面在乙個執行緒中進行多工切換的機制,比執行緒更加輕量級 實現併發量更大 協程的使用 使用第三方庫 gevent gevent 是乙個基於協程的 python 網路庫,在遇到 io 阻塞時,程式會自動進行切換,可以讓我們用同步的放肆寫非同步 io 協程的使用 from gevent im...