Wrox Programmer Forums
|
BOOK Programming Interviews Exposed: Secrets to Landing Your Next Job 3rd Edition
This is the forum to discuss the Wrox book Programming Interviews Exposed: Secrets to Landing Your Next Job, 3rd Edition by John Mongan, Noah Kindler, Eric Giguere; ISBN: 978-1-118-26136-1
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK Programming Interviews Exposed: Secrets to Landing Your Next Job 3rd Edition section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 6th, 2014, 02:42 AM
Registered User
 
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?
 
Old July 11th, 2017, 03:18 PM
Wrox Author
 
Join Date: Oct 2012
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Without the while loops, add will silently fail to add the value to the buffer if it is full, and remove will fail to retrieve a valid value from the queue if it is empty.

There are a variety of means that can be used to stop the consumer. One way to do it is called "poison pill": when the producer is ready to shut down it puts a special value on the queue just before it shuts down; when the consumer sees that value it shuts down. To make this work, you have to be sure that the special "poison pill" value is one that would never be placed on the queue in any other context, or you'll have the consumer shutting down when you don't want it to.

John





Similar Threads
Thread Thread Starter Forum Replies Last Post
.NET Interview Questions, C# Interview Questions, dotnetuncle .NET Framework 2.0 4 June 22nd, 2019 07:03 AM
Chapter 23 - Building the Consumer phil-c- BOOK: Professional VB 2005 ISBN: 0-7645-7536-8 1 January 20th, 2009 11:52 AM
Please help with these questions [email protected] C# 1 February 28th, 2008 07:14 PM
C++ questions jam93 C++ Programming 1 August 19th, 2007 10:39 PM
About data access provider and consumer kimsunshine ADO.NET 0 July 28th, 2006 06:45 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.