source:
src/tests/coroutine.c
@
39c5ea3
Last change on this file since 39c5ea3 was e04b636, checked in by , 8 years ago | |
---|---|
|
|
File size: 856 bytes |
Rev | Line | |
---|---|---|
[c15b805] | 1 | #include <fstream> |
[4a3334cf] | 2 | #include <coroutine> |
[c15b805] | 3 | |
[e04b636] | 4 | coroutine Fibonacci { |
[c15b805] | 5 | int fn; // used for communication |
6 | }; | |
7 | ||
[78b3f52] | 8 | void ?{}(Fibonacci* this) { |
9 | this->fn = 0; | |
[80d9e49] | 10 | } |
11 | ||
[7fbe450] | 12 | void main(Fibonacci* this) { |
[c15b805] | 13 | int fn1, fn2; // retained between resumes |
14 | this->fn = 0; | |
15 | fn1 = this->fn; | |
[9129a84] | 16 | suspend(); // return to last resume |
[c15b805] | 17 | |
18 | this->fn = 1; | |
19 | fn2 = fn1; | |
20 | fn1 = this->fn; | |
[9129a84] | 21 | suspend(); // return to last resume |
[c15b805] | 22 | |
23 | for ( ;; ) { | |
24 | this->fn = fn1 + fn2; | |
25 | fn2 = fn1; | |
26 | fn1 = this->fn; | |
[9129a84] | 27 | suspend(); // return to last resume |
[c15b805] | 28 | } |
29 | } | |
30 | ||
31 | int next(Fibonacci* this) { | |
32 | resume(this); // transfer to last suspend | |
33 | return this->fn; | |
34 | } | |
35 | ||
[9129a84] | 36 | int main() { |
[d9c44c3] | 37 | Fibonacci f1, f2; |
[c15b805] | 38 | for ( int i = 1; i <= 10; i += 1 ) { |
[d9c44c3] | 39 | sout | next(&f1) | ' ' | next(&f2) | endl; |
[c15b805] | 40 | } |
[9129a84] | 41 | |
42 | return 0; | |
[0e76cf4f] | 43 | } |
Note: See TracBrowser
for help on using the repository browser.