Changes in src/tests/coroutine.c [ec95d11:e04b636]
- File:
-
- 1 edited
-
src/tests/coroutine.c (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
src/tests/coroutine.c
rec95d11 re04b636 1 //2 // Cforall Version 1.0.0 Copyright (C) 2017 University of Waterloo3 //4 // The contents of this file are covered under the licence agreement in the5 // file "LICENCE" distributed with Cforall.6 //7 // fibonacci.c --8 //9 // Author : Thierry Delisle10 // Created On : Thu Jun 8 07:29:37 201711 // Last Modified By : Peter A. Buhr12 // Last Modified On : Thu Jun 8 07:37:12 201713 // Update Count : 514 //15 16 1 #include <fstream> 17 2 #include <coroutine> 18 3 19 4 coroutine Fibonacci { 20 int fn;// used for communication5 int fn; // used for communication 21 6 }; 22 7 23 void ?{}( Fibonacci * this) {24 this->fn = 0;8 void ?{}(Fibonacci* this) { 9 this->fn = 0; 25 10 } 26 11 27 void main( Fibonacci * this) {28 int fn1, fn2;// retained between resumes29 this->fn = 0; // case 0 30 fn1 = this->fn;31 suspend();// return to last resume12 void main(Fibonacci* this) { 13 int fn1, fn2; // retained between resumes 14 this->fn = 0; 15 fn1 = this->fn; 16 suspend(); // return to last resume 32 17 33 this->fn = 1; // case 1 34 fn2 = fn1;35 fn1 = this->fn;36 suspend();// return to last resume18 this->fn = 1; 19 fn2 = fn1; 20 fn1 = this->fn; 21 suspend(); // return to last resume 37 22 38 for ( ;; ) { // general case 39 this->fn = fn1 + fn2;40 fn2 = fn1;41 fn1 = this->fn;42 suspend();// return to last resume43 } // for 23 for ( ;; ) { 24 this->fn = fn1 + fn2; 25 fn2 = fn1; 26 fn1 = this->fn; 27 suspend(); // return to last resume 28 } 44 29 } 45 30 46 int next( Fibonacci * this) {47 resume( this );// transfer to last suspend48 return this->fn;31 int next(Fibonacci* this) { 32 resume(this); // transfer to last suspend 33 return this->fn; 49 34 } 50 35 51 36 int main() { 52 Fibonacci f1, f2; 53 for ( int i = 1; i <= 10; i += 1 ) { 54 sout | next( &f1 ) | ' ' | next( &f2 ) | endl; 55 } // for 37 Fibonacci f1, f2; 38 for ( int i = 1; i <= 10; i += 1 ) { 39 sout | next(&f1) | ' ' | next(&f2) | endl; 40 } 41 42 return 0; 56 43 } 57 58 // Local Variables: //59 // tab-width: 4 //60 // compile-command: "cfa fibonacci.c" //61 // End: //
Note:
See TracChangeset
for help on using the changeset viewer.