source: tests/coroutine/fibonacci.c @ bf71cfd

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprno_listpersistent-indexerpthread-emulationqualifiedEnum
Last change on this file since bf71cfd was bf71cfd, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Moved up many directories in source

  • Property mode set to 100644
File size: 1.3 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2017 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// fibonacci.c -- 3-state finite-state machine
8
9//
10// Author           : Thierry Delisle
11// Created On       : Thu Jun  8 07:29:37 2017
12// Last Modified By : Peter A. Buhr
13// Last Modified On : Fri Apr 27 08:55:31 2018
14// Update Count     : 19
15//
16
17#include <fstream>
18#include <coroutine>
19
20coroutine Fibonacci { int fn; };                                                // used for communication
21
22void main( Fibonacci & fib ) with( fib ) {                              // called on first resume
23        int fn1, fn2;                                                                           // retained between resumes
24        fn = 0;  fn1 = fn;                                                                      // 1st case
25        suspend();                                                                                      // restart last resume
26        fn = 1;  fn2 = fn1;  fn1 = fn;                                          // 2nd case
27        suspend();                                                                                      // restart last resume
28        for ( ;; ) {
29                fn = fn1 + fn2;  fn2 = fn1;  fn1 = fn;                  // general case
30                suspend();                                                                              // restart last resume
31        } // for
32}
33
34int next( Fibonacci & fib ) with( fib ) {
35        resume( fib );                                                                          // restart last suspend
36        return fn;
37}
38
39int main() {
40        Fibonacci f1, f2;
41        for ( int i = 1; i <= 10; i += 1 ) {
42                sout | next( f1 ) | next( f2 ) | endl;
43        } // for
44}
45
46// Local Variables: //
47// tab-width: 4 //
48// compile-command: "cfa fibonacci.c" //
49// End: //
Note: See TracBrowser for help on using the repository browser.