1 | #include <stdlib> |
---|
2 | #include <fstream> |
---|
3 | #include <kernel> |
---|
4 | #include <thread> |
---|
5 | #include <unistd.h> // getpid |
---|
6 | |
---|
7 | monitor Buffer { |
---|
8 | condition full, empty; |
---|
9 | int front, back, count; |
---|
10 | int elements[20]; |
---|
11 | }; |
---|
12 | |
---|
13 | void ?{}( Buffer & buffer ) { |
---|
14 | buffer.front = buffer.back = buffer.count = 0; |
---|
15 | } |
---|
16 | |
---|
17 | int query( Buffer & buffer ) { return buffer.count; } |
---|
18 | |
---|
19 | void insert( Buffer & mutex buffer, int elem ) { |
---|
20 | if ( buffer.count == 20 ) wait( &buffer.empty ); |
---|
21 | buffer.elements[buffer.back] = elem; |
---|
22 | buffer.back = ( buffer.back + 1 ) % 20; |
---|
23 | buffer.count += 1; |
---|
24 | signal( &buffer.full ); |
---|
25 | } |
---|
26 | int remove( Buffer & mutex buffer ) { |
---|
27 | if ( buffer.count == 0 ) wait( &buffer.full ); |
---|
28 | int elem = buffer.elements[buffer.front]; |
---|
29 | buffer.front = ( buffer.front + 1 ) % 20; |
---|
30 | buffer.count -= 1; |
---|
31 | signal( &buffer.empty ); |
---|
32 | return elem; |
---|
33 | } |
---|
34 | |
---|
35 | thread Producer { |
---|
36 | Buffer * buffer; |
---|
37 | unsigned int N; |
---|
38 | }; |
---|
39 | void main( Producer & prod ) { |
---|
40 | for ( int i = 1; i <= prod.N; i += 1 ) { |
---|
41 | yield( (unsigned int)rand48() % 5 ); |
---|
42 | insert( *prod.buffer, 1 ); |
---|
43 | } // for |
---|
44 | insert( *prod.buffer, -1 ); |
---|
45 | } |
---|
46 | void ?{}( Producer & prod, Buffer * buffer, unsigned int N ) { |
---|
47 | prod.buffer = buffer; |
---|
48 | prod.N = N; |
---|
49 | } |
---|
50 | |
---|
51 | thread Consumer { |
---|
52 | Buffer * buffer; |
---|
53 | int * sum; // summation of producer values |
---|
54 | }; |
---|
55 | void main( Consumer & cons ) { |
---|
56 | *cons.sum = 0; |
---|
57 | for ( ;; ) { |
---|
58 | yield( (unsigned int)rand48() % 5 ); |
---|
59 | int item = remove( *cons.buffer ); |
---|
60 | if ( item == -1 ) break; // sentinel ? |
---|
61 | *cons.sum += item; |
---|
62 | } // for |
---|
63 | } |
---|
64 | void ?{}( Consumer & cons, Buffer * buffer, int * sum ) { |
---|
65 | cons.buffer = buffer; |
---|
66 | cons.sum = sum; |
---|
67 | } |
---|
68 | |
---|
69 | int main() { |
---|
70 | Buffer buffer; |
---|
71 | enum { Prods = 5, Cons = 5 }; |
---|
72 | Producer * prods[Prods]; |
---|
73 | Consumer * cons[Cons]; |
---|
74 | const int Sentinel = -1; |
---|
75 | int sums[Cons]; |
---|
76 | int i; |
---|
77 | processor p; |
---|
78 | |
---|
79 | //rand48seed( getpid() ); |
---|
80 | rand48seed( 1003 ); |
---|
81 | |
---|
82 | for ( i = 0; i < Cons; i += 1 ) { // create consumers |
---|
83 | cons[i] = new( &buffer, &sums[i] ); |
---|
84 | } // for |
---|
85 | for ( i = 0; i < Prods; i += 1 ) { // create producers |
---|
86 | prods[i] = new( &buffer, 100000u ); |
---|
87 | } // for |
---|
88 | |
---|
89 | for ( i = 0; i < Prods; i += 1 ) { // wait for producers to finish |
---|
90 | delete( prods[i] ); |
---|
91 | } // for |
---|
92 | for ( i = 0; i < Cons; i += 1 ) { // generate sentinal values to stop consumers |
---|
93 | insert( buffer, Sentinel ); |
---|
94 | } // for |
---|
95 | int sum = 0; |
---|
96 | for ( i = 0; i < Cons; i += 1 ) { // wait for consumers to finish |
---|
97 | delete( cons[i] ); |
---|
98 | sum += sums[i]; |
---|
99 | } // for |
---|
100 | sout | "total:" | sum | endl; |
---|
101 | } |
---|