- Timestamp:
- Dec 12, 2018, 11:24:32 PM (6 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, no_list, persistent-indexer, pthread-emulation, qualifiedEnum
- Children:
- 24662ff, 9d6497a
- Parents:
- 5dd3098
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
tests/coroutine/prodcons.c
r5dd3098 r18cf979 10 10 // Created On : Mon Sep 18 12:23:39 2017 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue Dec 11 21:58:25201813 // Update Count : 5 212 // Last Modified On : Wed Dec 12 23:04:49 2018 13 // Update Count : 53 14 14 // 15 15 … … 24 24 25 25 coroutine Prod { 26 Cons *c;26 Cons & c; 27 27 int N, money, receipt; 28 28 }; … … 30 30 // 1st resume starts here 31 31 for ( i; N ) { // N pairs of values 32 int p1 = random( 100 ); 33 int p2 = random( 100 ); 32 int p1 = random( 100 ), p2 = random( 100 ); 34 33 sout | p1 | " " | p2; 35 int status = delivery( *c, p1, p2 ); 36 sout | " $" | money; 37 sout | status; 34 int status = delivery( c, p1, p2 ); 35 sout | " $" | money | nl | status; 38 36 receipt += 1; 39 37 } 40 stop( *c );38 stop( c ); 41 39 sout | "prod stops"; 42 40 } … … 47 45 } 48 46 void start( Prod & prod, int N, Cons &c ) { 49 prod.N = N; 50 prod.c = &c; 51 prod.receipt = 0; 47 &prod.c = &c; 48 prod.[N, receipt] = [N, 0]; 52 49 resume( prod ); // activate main 53 50 } 54 51 55 52 coroutine Cons { 56 Prod *p;53 Prod & p; 57 54 int p1, p2, status; 58 55 bool done; 59 56 }; 60 57 void ?{}( Cons & cons, Prod & p ) { 61 cons.p = &p; 62 cons.status = 0; 63 cons.done = false; 58 &cons.p = &p; 59 cons.[status, done ] = [0, false]; 64 60 } 65 61 void ^?{}( Cons & cons ) {} … … 68 64 int money = 1, receipt; 69 65 for ( ; ! done; ) { 70 sout | p1 | " " | p2; 71 sout | " $" | money; 66 sout | p1 | " " | p2 | nl | " $" | money; 72 67 status += 1; 73 receipt = payment( *p, money );68 receipt = payment( p, money ); 74 69 sout | " #" | receipt; 75 70 money += 1; … … 78 73 } 79 74 int delivery( Cons & cons, int p1, int p2 ) { 80 cons.p1 = p1; 81 cons.p2 = p2; 75 cons.[p1, p2] = [p1, p2]; 82 76 resume( cons ); // main 1st time, then 83 77 return cons.status; // cons in payment
Note: See TracChangeset
for help on using the changeset viewer.