source: CIVL/examples/concurrency/boundedBuffer.cvl@ 5bc08d6

1.23 2.0 main test-branch
Last change on this file since 5bc08d6 was 6eefc77, checked in by Manchun Zheng <zmanchun@…>, 11 years ago

cleaned up concurrency examples.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@2268 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 1.4 KB
RevLine 
[6eefc77]1/**
2 * boundedBuffer.cvl:
3 * This program impelments a bounded-buffer.
4 * It describes two processes, the producer and consumer,
5 * which share a common, fixed-size buffer
6 * used as a queue.
7 *
8 * The producer's job is to generate a piece of data,
9 * put it into the buffer and start again.
10 * At the same time, the consumer is consuming the data
11 * (i.e., removing it from the buffer) one piece at a time. *
12 * The problem is to make sure that the producer won't try
13 * to add data into the buffer if it's full and that the
14 * consumer won't try to remove data from an empty buffer.
15 */
16#include<civlc.cvh>
17$input int BUFFER_SIZE = 50;
18$input int NPRODUCER = 2;
19$input int NCONSUMER = 2;
20int fillCount = 0;
21int emptyCount = BUFFER_SIZE;
22
23void producer() {
24 while ($true) {
25 // produce item
26 $when(emptyCount > 0);
27 $assert(emptyCount > 0);
28 emptyCount--;
29 // put item into buffer
30 fillCount++;
31 }
32}
33
34void consumer() {
35 while ($true) {
36 $when(fillCount > 0);
37 $assert(fillCount > 0);
38 fillCount--;
39 // remove item from buffer
40 emptyCount++;
41 // consume item
42 }
43}
44
45void main(){
46 $proc producers[NPRODUCER];
47 $proc consumers[NCONSUMER];
48
49 for(int i = 0; i < NCONSUMER; i++)
50 consumers[i] = $spawn consumer();
51 for(int i = 0; i < NPRODUCER; i++)
52 producers[i] = $spawn producer();
53 $waitall(consumers, NCONSUMER);
54 $waitall(producers, NPRODUCER);
55}
Note: See TracBrowser for help on using the repository browser.