/
test.cpp
113 lines (83 loc) · 1.86 KB
/
test.cpp
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "coroutine.h"
#include <iostream>
using namespace std;
Coro_t co1, co2;
void func1()
{
int a = 10;
cout << "hello func1" << endl;
Coro_t co = coroutine.running();
if(co != co1)
{
cout << "error, co1 must run" << endl;
exit(1);
}
coroutine.yield();
co = coroutine.running();
if(co != co1)
{
cout << "error, co1 must run" << endl;
exit(1);
}
cout << "hello end func1\t" << a << endl;
}
void func2(Coro_t id)
{
int a = 100;
cout << "hello func2" << endl;
Coro_t co = coroutine.running();
if(co != co2)
{
cout << "error, co2 must run" << endl;
exit(1);
}
CoroStatus s = coroutine.status(co1);
if(s != Coro_Suspended)
{
cout << "error, co1 must suspeneded" << endl;
exit(1);
}
coroutine.resume(id);
s = coroutine.status(co1);
if(s != Coro_Dead)
{
cout << "error, co1 must daed" << endl;
exit(1);
}
co = coroutine.running();
if(co != co2)
{
cout << "error, co2 must run" << endl;
exit(1);
}
coroutine.yield();
cout << "hello end func2\t" << a << endl;
}
void func()
{
cout << "hello func" << endl;
co1 = coroutine.create(std::bind(&func1));
coroutine.resume(co1);
co2 = coroutine.create(std::bind(&func2, co1));
coroutine.resume(co2);
coroutine.resume(co2);
CoroStatus s = coroutine.status(co2);
if(s != Coro_Dead)
{
cout << "error, co2 must dead" << endl;
exit(1);
}
cout << "hello end fun" << endl;
}
int main()
{
if(coroutine.running() != 0)
{
cout << "error, main thread not a coroutine" << endl;
exit(1);
}
Coro_t co = coroutine.create(std::bind(&func));
coroutine.resume(co);
cout << "end" << endl;
return 0;
}