Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old March 6th, 2014, 02:42 AM
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
  #2 (permalink)  
Old July 11th, 2017, 04:18 PM
Wrox Author
Points: 21, Level: 1
Points: 21, Level: 1 Points: 21, Level: 1 Points: 21, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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
Reply With Quote
  #3 (permalink)  
Old July 20th, 2017, 08:32 AM
Banned
Points: 3, Level: 1
Points: 3, Level: 1 Points: 3, Level: 1 Points: 3, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2017
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Where to check Nigeriens day today updated news?

You can check the Latest news in Nigeria only from YNaija because ynaija is the internet newspaper for young Nigerians, focused on the issues and ideas that matter for an evolving generation.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
.NET Interview Questions, C# Interview Questions, dotnetuncle .NET Framework 2.0 3 April 9th, 2015 08:43 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 AccessAasif@aol.com C# 1 February 28th, 2008 07:14 PM
C++ questions jam93 C++ Programming 1 August 19th, 2007 11:39 PM
About data access provider and consumer kimsunshine ADO.NET 0 July 28th, 2006 07:45 AM



All times are GMT -4. The time now is 05:56 PM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.