source:
tests/coroutine/runningTotal.cfa@
2d743bd
| Last change on this file since 2d743bd was 427854b, checked in by , 6 years ago | |
|---|---|
|
|
| File size: 1.2 KB | |
| Rev | Line | |
|---|---|---|
| [73abe95] | 1 | // |
| [92494fd] | 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. | |
| [73abe95] | 6 | // |
| 7 | // runningTotal.c -- | |
| 8 | // | |
| [92494fd] | 9 | // Author : Peter A. Buhr |
| 10 | // Created On : Wed Dec 6 08:05:27 2017 | |
| 11 | // Last Modified By : Peter A. Buhr | |
| [f8cd310] | 12 | // Last Modified On : Fri Mar 22 13:40:49 2019 |
| 13 | // Update Count : 5 | |
| [73abe95] | 14 | // |
| [92494fd] | 15 | |
| [73abe95] | 16 | #include <fstream.hfa> |
| 17 | #include <coroutine.hfa> | |
| [92494fd] | 18 | |
| 19 | coroutine RunTotal { // input numbers and return running total | |
| 20 | int input, total; // communication | |
| 21 | }; | |
| 22 | ||
| 23 | void ?{}( RunTotal & rntl ) { rntl.total = 0; } | |
| 24 | ||
| 25 | void update( RunTotal & rntl, int input ) with( rntl ) { // helper | |
| 26 | total += input; // remember between activations | |
| [427854b] | 27 | suspend; // inactivate on stack |
| [92494fd] | 28 | } |
| 29 | ||
| 30 | void main( RunTotal & rntl ) with( rntl ) { | |
| [adb6b30f] | 31 | for () { |
| [92494fd] | 32 | update( rntl, input ); |
| 33 | } // for | |
| 34 | } | |
| 35 | ||
| 36 | int add( RunTotal & rntl, int input ) { | |
| 37 | rntl.input = input; // pass input to coroutine | |
| 38 | resume( rntl ); | |
| 39 | return rntl.total; // return total from coroutine | |
| 40 | } | |
| 41 | int main() { | |
| 42 | RunTotal rntl; | |
| [adb6b30f] | 43 | for ( i; 10 ) { |
| [200fcb3] | 44 | sout | i | add( rntl, i ); |
| [92494fd] | 45 | } // for |
| 46 | } | |
| 47 | ||
| 48 | // Local Variables: // | |
| 49 | // tab-width: 4 // | |
| [f8cd310] | 50 | // compile-command: "cfa runningTotal.cfa" // |
| [92494fd] | 51 | // End: // |
Note:
See TracBrowser
for help on using the repository browser.