source: tests/coroutine/suspend_then.cfa

Last change on this file was dfa4360, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

Added several tests for generators

  • Property mode set to 100644
File size: 1.2 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2019 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// suspend_then.cfa --
8//
9// Author           : Peter A. Buhr
10// Created On       : Mon Apr 29 12:01:35 2019
11// Last Modified By :
12// Last Modified On :
13// Update Count     :
14//
15
16#include <fstream.hfa>
17
18generator Fibonacci {
19        int fn;                                                                         // used for communication
20        int fn1, fn2;                                                           // retained between resumes
21};
22
23void main( Fibonacci & fib ) with( fib ) {                              // called on first resume
24        fn = 0;  fn1 = fn;                                                      // 1st case
25        suspend { sout | "Then!"; }                                             // restart last resume
26        fn = 1;  fn2 = fn1;  fn1 = fn;                                  // 2nd case
27        suspend { sout | "Then!"; }                                             // restart last resume
28        for () {
29                fn = fn1 + fn2;  fn2 = fn1;  fn1 = fn;                  // general case
30                suspend { sout | "Then!"; }                                     // 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 ( 10 ) {                                                            // print N Fibonacci values
42                sout | next( f1 ) | next( f2 );
43        } // for
44}
45
46// Local Variables: //
47// tab-width: 4 //
48// compile-command: "cfa runningTotal.cfa" //
49// End: //
Note: See TracBrowser for help on using the repository browser.