Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int N=10;
- int countvalues = 1;
- monitor monitor1
- {
- int buffer[N];
- int write_pos;
- int read_pos;
- int count;
- condition full,empty;
- void queue_add(int item)
- {
- if(count == N)
- {
- waitc(full);
- }
- buffer[write_pos] = item;
- write_pos = (write_pos + 1) % N;
- count++;
- signalc(empty);
- }
- int queue_get()
- {
- int item;
- if(count == 0)
- {
- waitc(empty);
- }
- item = buffer[read_pos];
- read_pos = (read_pos + 1) % N;
- count--;
- countvalues++;
- signalc(full);
- return item;
- }
- init
- {
- read_pos = 0;
- write_pos = 0;
- count = 0;
- }
- }
- void producer(int size)
- {
- int i;
- for(i=0; i<size; i++)
- {
- queue_add(i);
- }
- }
- void consumer(int size)
- {
- int i;
- for(i=0; i<size; i++)
- {
- cout<<"Value number "<<countvalues<<": "<<queue_get()<<endl;
- }
- }
- main()
- {
- cout<<"N="<<N<<endl;
- cout<<endl;
- cobegin
- {
- producer(N);
- producer(N);
- producer(N);
- consumer(3*N);
- }
- cout<<"All values consumed."<<endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement