source: doc/papers/concurrency/examples/DatingServiceThread.cfa@ 24a3d2b

ADT ast-experimental pthread-emulation qualifiedEnum
Last change on this file since 24a3d2b was 2aab69b, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

files added to concurrency paper

  • Property mode set to 100644
File size: 3.1 KB
Line 
1#include <stdlib.hfa> // random
2#include <fstream.hfa>
3#include <kernel.hfa>
4#include <thread.hfa>
5#include <unistd.h> // getpid
6
7enum { CompCodes = 20 }; // number of compatibility codes
8
9thread DatingService {
10 condition Girls[CompCodes], Boys[CompCodes];
11 unsigned int girlPhoneNo, boyPhoneNo, ccode;
12}; // DatingService
13
14unsigned int girl( DatingService & mutex ds, unsigned int phoneno, unsigned int code ) with( ds ) {
15 girlPhoneNo = phoneno; ccode = code;
16 wait( Girls[ccode] ); // wait for boy
17 girlPhoneNo = phoneno;
18 sout | "Girl:" | girlPhoneNo | "is dating Boy at" | boyPhoneNo | "with ccode" | ccode;
19 return boyPhoneNo;
20} // DatingService girl
21
22unsigned int boy( DatingService & mutex ds, unsigned int phoneno, unsigned int code ) with( ds ) {
23 boyPhoneNo = phoneno; ccode = code;
24 wait( Boys[ccode] ); // wait for girl
25 boyPhoneNo = phoneno;
26 sout | " Boy:" | boyPhoneNo | "is dating Girl" | girlPhoneNo | "with ccode" | ccode;
27 return girlPhoneNo;
28} // DatingService boy
29
30void main( DatingService & ds ) with( ds ) { // thread starts
31 for () {
32 waitfor( ^?{} : ds ) {
33 break;
34 } or waitfor( girl : ds ) {
35 if ( ! is_empty( Boys[ccode] ) ) { // no compatible boy ?
36 signal_block( Boys[ccode] ); // restart boy to set phone number
37 signal_block( Girls[ccode] ); // restart girl to set phone number
38 } // if
39 } or waitfor( boy : ds ) {
40 if ( ! is_empty( Girls[ccode] ) ) { // no compatible girl ?
41 signal_block( Girls[ccode] ); // restart girl to set phone number
42 signal_block( Boys[ccode] ); // restart boy to set phone number
43 } // if
44 }
45 }
46} // DatingService main
47
48unsigned int girlck[CompCodes];
49unsigned int boyck[CompCodes];
50
51thread Girl {
52 DatingService & TheExchange;
53 unsigned int id, ccode;
54}; // Girl
55
56void main( Girl & g ) with( g ) {
57 yield( random( 100 ) ); // do not start at the same time
58 unsigned int partner = girl( TheExchange, id, ccode );
59 girlck[id] = partner;
60} // Girl main
61
62void ?{}( Girl & g, DatingService * TheExchange, unsigned int id, unsigned int ccode ) {
63 &g.TheExchange = TheExchange;
64 g.id = id;
65 g.ccode = ccode;
66} // Girl ?{}
67
68thread Boy {
69 DatingService & TheExchange;
70 unsigned int id, ccode;
71}; // Boy
72
73void main( Boy & b ) with( b ) {
74 yield( random( 100 ) ); // don't all start at the same time
75 unsigned int partner = boy( TheExchange, id, ccode );
76 boyck[id] = partner;
77} // Boy main
78
79void ?{}( Boy & b, DatingService * TheExchange, unsigned int id, unsigned int ccode ) {
80 &b.TheExchange = TheExchange;
81 b.id = id;
82 b.ccode = ccode;
83} // Boy ?{}
84
85int main() {
86 DatingService TheExchange;
87 Girl * girls[CompCodes];
88 Boy * boys[CompCodes];
89
90 srandom( /*getpid()*/ 103 );
91
92 for ( i; (unsigned int)CompCodes ) {
93 girls[i] = new( &TheExchange, i, i ); // TheExchange constructor needs unsigned int
94 boys[i] = new( &TheExchange, i, CompCodes - ( i + 1 ) );
95 } // for
96
97 for ( i; CompCodes ) {
98 delete( boys[i] );
99 delete( girls[i] );
100 } // for
101
102 for ( i; CompCodes ) {
103 if ( girlck[ boyck[i] ] != boyck[ girlck[i] ] ) abort();
104 } // for
105} // main
106
107// Local Variables: //
108// tab-width: 4 //
109// compile-command: "cfa DatingServiceThread.cfa" //
110// End: //
Note: See TracBrowser for help on using the repository browser.