说到c++上的协程,boost里其实已经有相关的实现了,不过接口上看用起来有些麻烦,单纯从语法上来说,我觉得Lua的协程最简洁易用了,概念上也比较直接,为什么不做一个类似的呢?所以我就打算照着Lua来山寨一个,只需要支持四个接口就够了:
1)create coroutine。
2)run/resume coroutine。
3)Yield running corouinte。
4)IsCoroutineAlive。
保存与恢复上下文
实现协程/线程,最麻烦莫过于保存和切换上下文了,好在makecontext,swapcontext这几个函数相当好用,已经完全帮忙解决了这个难题:makecontext可以帮我们建立起协程的上下文,swapcontext则可以切换不同的上下文,从而实现那种把当前函数暂时停住,切换出去执行别的函数然后再切换回来继续执行的效果:
复制代码
#include
#include
using namespace std;
static char g_stack[2048];
static ucontext_t ctx,ctx_main;
void func()
{
// do something.
cout << "enter func" << endl;
swapcontext(&ctx, &ctx_main);
cout << "func1 resume from yield" << endl;
// continue to do something.
}
int main()
{
getcontext(&ctx);
ctx.uc_stack.ss_sp = g_stack;
ctx.uc_stack.ss_size = sizeof g_stack;
ctx.uc_link = &ctx_main;
makecontext(&ctx, func, 0);
cout << "in main, before coroutine starts" << endl;
// §è func.
swapcontext(&ctx_main, &ctx);
cout << "back to main" << endl;
// § §è func.
swapcontext(&ctx_main, &ctx);
cout << "back to main again" << endl;
return 0;
}
复制代码
如上代码所示,显然我们只要简单包装一下swapcontext,很容易就可以实现Yield和Resume,有了它们的帮助协程做起来就容易多了。
使用与实现
在使用makecontext,swapcontext的基础上,我花了一个多小时简单实现了一个协程库,参看这里,代码写下来总共才200多行,出乎意料的简单,用起来也很方便了:
复制代码
#include "coroutine.h"
#include
using namespace std;
CoroutineScheduler* sched = NULL;
void func1(void* arg)
{
uintptr_t ret;
cout << "function1 a now!,arg:" << arg << ", start to yield." << endl;
ret = sched->Yield((uintptr_t)"func1 yield 1");
cout << "1.fun1 return from yield:" << (const char*)ret << endl;
ret = sched->Yield((uintptr_t)"func1 yield 2");
cout << "2.fun1 return from yield:" << (const char*)ret << ", going to stop" << endl;
}
void func2(void* s)
{
cout << "function2 a now!, arg:" << s << ", start to yield." << endl;
const char* y = (const char*)sched->Yield((uintptr_t)"func2 yield 1");
cout << "fun2 return from yield:" << y <<", going to stop" << endl;
}
int main()
{
sched = new CoroutineScheduler();
bool stop = false;
int f1 = sched->CreateCoroutine(func1, (void*)111);
int f2 = sched->CreateCoroutine(func2, (void*)222);
while (!stop)
{
stop = true;
if (sched->IsCoroutineAlive(f1))
{
stop = false;
const char* y1 = (const char*)sched->ResumeCoroutine(f1, (uintptr_t)"resume func1");
cout << "func1 yield:" << y1 << endl;
}
if (sched->IsCoroutineAlive(f2))
{
stop = false;
const char* y2 = (const char*)sched->ResumeCoroutine(f2, (uintptr_t)"resume func2");
cout << "func2 yield:" << y2 << endl;
}
}
delete sched;
return 0;
}
复制代码
如上所示,Yield里传的参数会在调用Resume时被返回,同理Resume里的第二个参数,会在Yield里被返回,这种机制也是模仿Lua来的,有些时候可以用来在协程间传递一些参数,很方便。
这个协程看起来挺酷的,实现上却相当的简洁,核心代码如下:
复制代码
// static function
void CoroutineScheduler::SchedulerImpl::Schedule(void* arg)
{
assert(arg);
SchedulerImpl* sched = (SchedulerImpl*) arg;
int running = sched->running_;
coroutine* cor = sched->id2routine_[running];
assert(cor);
cor->func(cor->arg);
sched->running_ = -1;
cor->status = CO_FINISHED;
}
// resume coroutine.
uintptr_t CoroutineScheduler::Sc