source: src/tests/coroutine/runningTotal.c @ 1bdd261

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumwith_gc
Last change on this file since 1bdd261 was 92494fd, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

add coroutine test runningTotal

  • 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// runningTotal.c --
8//
9// Author           : Peter A. Buhr
10// Created On       : Wed Dec  6 08:05:27 2017
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Wed Dec  6 08:09:24 2017
13// Update Count     : 2
14//
15
16#include <fstream>
17#include <coroutine>
18
19coroutine RunTotal {                                                                    // input numbers and return running total
20        int input, total;                                                                       // communication
21};
22
23void ?{}( RunTotal & rntl ) { rntl.total = 0; }
24
25void update( RunTotal & rntl, int input ) with( rntl ) { // helper
26        total += input;                                                                         // remember between activations
27        suspend();                                                                                      // inactivate on stack
28}
29
30void main( RunTotal & rntl ) with( rntl ) {
31        for ( ;; ) {
32                update( rntl, input );
33        } // for
34}
35
36int 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}
41int main() {
42        RunTotal rntl;
43        for ( int i = 0; i < 10; i += 1 ) {
44                sout | i | add( rntl, i ) | endl;
45        } // for
46}
47
48// Local Variables: //
49// tab-width: 4 //
50// compile-command: "cfa runningTotal.c" //
51// End: //
Note: See TracBrowser for help on using the repository browser.