-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcomain.cc
59 lines (46 loc) · 1.52 KB
/
comain.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "coroutine.h"
#include <iostream>
using namespace std;
CoroutineScheduler* sched = NULL;
uintptr_t 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;
return (uintptr_t)"func1 stop";
}
uintptr_t 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;
return (uintptr_t)"func2 stop";
}
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;
}