View Single Post
  #1 (permalink)  
Old March 6th, 2014, 01:42 AM
flethuseo flethuseo is offline
Registered User
Points: 10, Level: 1
Points: 10, Level: 1 Points: 10, Level: 1 Points: 10, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2014
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default questions on Producer Consumer

Hi all,

1. In the code for the producer/consumer without any synchronization, why is there a 'while true' in the add and remove methods? I have removed it and it seems to work better:
Code:
		private void add(int num) {
			if (index < buffer.length) {
				buffer[index++] = num;
				return;
			}
		}

		private int remove() {
			if (index > 0) {
				return buffer[--index];
			}
			return -1;
		}

2. After synchronizing access to the buffer for the producer and consumer. What would be the best way to make the consumer stop when the producer stops producing and the queue is empty?
Reply With Quote