source: CIVL/examples/concurrency/boundedBuffer.cvl@ 3689f18f

1.23 2.0 main test-branch
Last change on this file since 3689f18f was 01533d1, checked in by Ziqing Luo <ziqing@…>, 10 years ago

fixed bug in boundedBuffer.cvl. It is same as boundedBuffer_bad.
The $when is not used correctly

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

  • Property mode set to 100644
File size: 1.4 KB
Line 
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 }
29 emptyCount--;
30 // put item into buffer
31 fillCount++;
32 }
33}
34
35void consumer() {
36 while ($true) {
37 $when(fillCount > 0){
38 $assert(fillCount > 0);
39 }
40 fillCount--;
41 // remove item from buffer
42 emptyCount++;
43 // consume item
44 }
45}
46
47void main(){
48 $proc producers[NPRODUCER];
49 $proc consumers[NCONSUMER];
50
51 for(int i = 0; i < NCONSUMER; i++)
52 consumers[i] = $spawn consumer();
53 for(int i = 0; i < NPRODUCER; i++)
54 producers[i] = $spawn producer();
55 $waitall(consumers, NCONSUMER);
56 $waitall(producers, NPRODUCER);
57}
Note: See TracBrowser for help on using the repository browser.