<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" media="screen" href="/styles/xslt/rss.xslt"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:media="http://search.yahoo.com/mrss/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:c9="http://channel9.msdn.com">
<channel>
	<title>Comment Feed for Channel 9 - BlockingCollection&amp;#60T&amp;#62 Demonstration in Producer-Consumer Scenarios</title>
	<atom:link rel="self" type="application/rss+xml" href="http://channel9.msdn.com/Blogs/philpenn/BlockingCollectionT-Demonstration-in-Producer-Consumer-Scenarios/RSS"></atom:link>
	<image>
		<url>http://ecn.channel9.msdn.com/o9/previewImages/100/503055_100x75.jpg</url>
		<title>Channel 9 - BlockingCollection&amp;#60T&amp;#62 Demonstration in Producer-Consumer Scenarios</title>
		<link></link>
	</image>
	<description>Join Josh and Steve as they demonstrate how to use the new .NET4 BlockingCollection&amp;lt;T&amp;gt; class in class Producer/Consumer parallel computing scenarios.BlockingCollection&amp;lt;T&amp;gt; is one of many new thread-safe data-structures available with .NET4 and Visual Studio 2010.Learn more about the 
System.Collections.Concurrent namespace and keep abreast of Parallel Computing tools and techniques via the

Concurrency Dev Center.See all videos in this 
series. 
</description>
	<link></link>
	<language>en</language>
	<pubDate>Thu, 23 May 2013 04:56:56 GMT</pubDate>
	<lastBuildDate>Thu, 23 May 2013 04:56:56 GMT</lastBuildDate>
	<generator>Rev9</generator>
	<item>
		<title>Re: BlockingCollection&amp;#60T&amp;#62 Demonstration in Producer-Consumer Scenarios</title>
		<description>
			<![CDATA[
<p>so does the concurrent collection remove values after they've been retrived?</p>
<p>if you dont want multiple consumers to process the same data, thats a good thing, but what if you do? like in a logger for instance, the app produces a bunch of messages, all the consumers want all the messages, and maybe a while after they've been produced.
 there migt be a log window in the app, but also a remote monitoring app for example</p>
<p>&nbsp;</p>
<p>how would you go about solving that? </p>
<p>posted by aL_</p>]]>
		</description>
		<link>http://channel9.msdn.com/Blogs/philpenn/BlockingCollectionT-Demonstration-in-Producer-Consumer-Scenarios#c633924959780000000</link>
		<pubDate>Fri, 30 Oct 2009 10:39:38 GMT</pubDate>
		<guid isPermaLink="true">http://channel9.msdn.com/Blogs/philpenn/BlockingCollectionT-Demonstration-in-Producer-Consumer-Scenarios#c633924959780000000</guid>
		<dc:creator>aL_</dc:creator>
	</item>
	<item>
		<title>Re: BlockingCollection&amp;#60T&amp;#62 Demonstration in Producer-Consumer Scenarios</title>
		<description>
			<![CDATA[
<p><em>re: does the concurrent collection remove values after they've been retrieved?</em></p>
<p>&nbsp;</p>
<p>If you just do:</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; foreach(var item in blockingCollection) { ... }</p>
<p>you'll be enumerating the contents without removing the values.&nbsp; If you do:</p>
<p>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; foreach(var item in blockingCollection.GetConsumingEnumerable()) { ... }</p>
<p>then you'll be enumerating the contents and removing the values as you do.</p>
<p>posted by Toub</p>]]>
		</description>
		<link>http://channel9.msdn.com/Blogs/philpenn/BlockingCollectionT-Demonstration-in-Producer-Consumer-Scenarios#c633924987900000000</link>
		<pubDate>Fri, 30 Oct 2009 11:26:30 GMT</pubDate>
		<guid isPermaLink="true">http://channel9.msdn.com/Blogs/philpenn/BlockingCollectionT-Demonstration-in-Producer-Consumer-Scenarios#c633924987900000000</guid>
		<dc:creator>Toub</dc:creator>
	</item>
	<item>
		<title>Re: BlockingCollection&amp;#60T&amp;#62 Demonstration in Producer-Consumer Scenarios</title>
		<description>
			<![CDATA[
<p>Why does BlockingCollection&lt;T&gt; not expose an event for the consumer?</p>
<p>posted by gdesroches</p>]]>
		</description>
		<link>http://channel9.msdn.com/Blogs/philpenn/BlockingCollectionT-Demonstration-in-Producer-Consumer-Scenarios#c633925027040000000</link>
		<pubDate>Fri, 30 Oct 2009 12:31:44 GMT</pubDate>
		<guid isPermaLink="true">http://channel9.msdn.com/Blogs/philpenn/BlockingCollectionT-Demonstration-in-Producer-Consumer-Scenarios#c633925027040000000</guid>
		<dc:creator>gdesroches</dc:creator>
	</item>
	<item>
		<title>Re: BlockingCollection&amp;#60T&amp;#62 Demonstration in Producer-Consumer Scenarios</title>
		<description>
			<![CDATA[
<p>If I have multiple producers and one of them calls bc.CompleteAdding() wouldn't it be possible for the consumer to exit before the other consumers have finished their bc.Add() calls? Am I misunderstanding the behavior?</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>posted by GrantB</p>]]>
		</description>
		<link>http://channel9.msdn.com/Blogs/philpenn/BlockingCollectionT-Demonstration-in-Producer-Consumer-Scenarios#c633925049760000000</link>
		<pubDate>Fri, 30 Oct 2009 13:09:36 GMT</pubDate>
		<guid isPermaLink="true">http://channel9.msdn.com/Blogs/philpenn/BlockingCollectionT-Demonstration-in-Producer-Consumer-Scenarios#c633925049760000000</guid>
		<dc:creator>GrantB</dc:creator>
	</item>
	<item>
		<title>Re: BlockingCollection&amp;#60T&amp;#62 Demonstration in Producer-Consumer Scenarios</title>
		<description>
			<![CDATA[
<p>Just a minor thing, you said &quot;all Five are output&quot; but in the Console window there are only 4.&nbsp;&nbsp; Doh!</p>
<p>posted by AdamSpeight2008</p>]]>
		</description>
		<link>http://channel9.msdn.com/Blogs/philpenn/BlockingCollectionT-Demonstration-in-Producer-Consumer-Scenarios#c633925182760000000</link>
		<pubDate>Fri, 30 Oct 2009 16:51:16 GMT</pubDate>
		<guid isPermaLink="true">http://channel9.msdn.com/Blogs/philpenn/BlockingCollectionT-Demonstration-in-Producer-Consumer-Scenarios#c633925182760000000</guid>
		<dc:creator>AdamSpeight2008</dc:creator>
	</item>
	<item>
		<title>Re: BlockingCollection&amp;#60T&amp;#62 Demonstration in Producer-Consumer Scenarios</title>
		<description>
			<![CDATA[
<p>I was just about to ask the same thing <img src='http://ecn.channel9.msdn.com/o9/content/images/emoticons/emotion-1.gif' alt='Smiley' /></p>
<p>posted by RTaylor-UTS</p>]]>
		</description>
		<link>http://channel9.msdn.com/Blogs/philpenn/BlockingCollectionT-Demonstration-in-Producer-Consumer-Scenarios#c633925268050000000</link>
		<pubDate>Fri, 30 Oct 2009 19:13:25 GMT</pubDate>
		<guid isPermaLink="true">http://channel9.msdn.com/Blogs/philpenn/BlockingCollectionT-Demonstration-in-Producer-Consumer-Scenarios#c633925268050000000</guid>
		<dc:creator>RTaylor-UTS</dc:creator>
	</item>
	<item>
		<title>Re: BlockingCollection&amp;#60T&amp;#62 Demonstration in Producer-Consumer Scenarios</title>
		<description>
			<![CDATA[
<p>&quot;If I have multiple producers and one of them calls bc.CompleteAdding() wouldn't it be possible for the consumer to exit before the other consumers have finished their bc.Add() calls? Am I misunderstanding the behavior?&quot;</p>
<p>&nbsp;</p>
<p>If you call CompleteAdding(), any more calls to Add() will throw.&nbsp; Consumers will run until q empty and will throw on Take().&nbsp; For multiple producers, you probably need a master that can signal producers to stop and then master can exit clean with a CompleteAdding.&nbsp;
 Consumers can catch the cancel exception or use TryTake.&nbsp; For that matter, the producers could use TryAdd as well.&nbsp; Then other producers could exit cleanly and you may not need an out-of-band signal in that case.</p>
<p>posted by staceyw</p>]]>
		</description>
		<link>http://channel9.msdn.com/Blogs/philpenn/BlockingCollectionT-Demonstration-in-Producer-Consumer-Scenarios#c633925620440000000</link>
		<pubDate>Sat, 31 Oct 2009 05:00:44 GMT</pubDate>
		<guid isPermaLink="true">http://channel9.msdn.com/Blogs/philpenn/BlockingCollectionT-Demonstration-in-Producer-Consumer-Scenarios#c633925620440000000</guid>
		<dc:creator>staceyw</dc:creator>
	</item>
	<item>
		<title>Re: BlockingCollection&amp;#60T&amp;#62 Demonstration in Producer-Consumer Scenarios</title>
		<description>
			<![CDATA[
<p>re: &quot;you said &quot;all Five are output&quot; but in the Console window there are only 4.&nbsp;&nbsp; Doh!&quot;</p>
<p>&nbsp;</p>
<p>It actually did output all five.&nbsp; Unfortunately the screen capture software appears to only have been taking appx one image per second, and it didn't take a snapshot between the time that the consumer output its fourth value and the time that the console
 window closed... in that time window the 5th element did appear.&nbsp; You can visibly see this delay in several other portions of the video, such as when I'm typing (and portions of words are magically appearing in chunks <img src='http://ecn.channel9.msdn.com/o9/content/images/emoticons/emotion-5.gif' alt='Wink' />) or when dragging around the console
 window.</p>
<p>posted by Toub</p>]]>
		</description>
		<link>http://channel9.msdn.com/Blogs/philpenn/BlockingCollectionT-Demonstration-in-Producer-Consumer-Scenarios#c633925958980000000</link>
		<pubDate>Sat, 31 Oct 2009 14:24:58 GMT</pubDate>
		<guid isPermaLink="true">http://channel9.msdn.com/Blogs/philpenn/BlockingCollectionT-Demonstration-in-Producer-Consumer-Scenarios#c633925958980000000</guid>
		<dc:creator>Toub</dc:creator>
	</item>
	<item>
		<title>Re: BlockingCollection&amp;#60T&amp;#62 Demonstration in Producer-Consumer Scenarios</title>
		<description>
			<![CDATA[
<p>For multiple producers where you want to call complete adding when they're all done, TPL's coordination constructs can come in very handy.&nbsp; e.g. I can use a ContinueWhenAll to call CompleteAdding:</p>
<p>&nbsp;</p>
<p>var bc = new BlockingCollection&lt;int&gt;(); </p>
<p>var producers = new List&lt;Task&gt;(); </p>
<p>for(int i=0; i&lt;numProducers; i&#43;&#43;)</p>
<p>{ </p>
<p>&nbsp;&nbsp;&nbsp; producers.Add(Task.Factory.StartNew(() =&gt; /* produce data to bc here */));
</p>
<p>} </p>
<p>Task.Factory.ContinueWhenAll(producers.ToArray(), _ =&gt; bc.CompleteAdding());</p>
<p>posted by Toub</p>]]>
		</description>
		<link>http://channel9.msdn.com/Blogs/philpenn/BlockingCollectionT-Demonstration-in-Producer-Consumer-Scenarios#c633925961840000000</link>
		<pubDate>Sat, 31 Oct 2009 14:29:44 GMT</pubDate>
		<guid isPermaLink="true">http://channel9.msdn.com/Blogs/philpenn/BlockingCollectionT-Demonstration-in-Producer-Consumer-Scenarios#c633925961840000000</guid>
		<dc:creator>Toub</dc:creator>
	</item>
	<item>
		<title>Re: BlockingCollection&amp;#60T&amp;#62 Demonstration in Producer-Consumer Scenarios</title>
		<description>
			<![CDATA[
<p>&quot; _ =&gt;&quot;</p>
<p>&nbsp;</p>
<p>I do no follow the underline here.&nbsp; Is this something new?</p>
<p>Also, what is the best way to get TPL now?&nbsp; In a library or wait for .net 4.0?</p>
<p>posted by staceyw</p>]]>
		</description>
		<link>http://channel9.msdn.com/Blogs/philpenn/BlockingCollectionT-Demonstration-in-Producer-Consumer-Scenarios#c633926133850000000</link>
		<pubDate>Sat, 31 Oct 2009 19:16:25 GMT</pubDate>
		<guid isPermaLink="true">http://channel9.msdn.com/Blogs/philpenn/BlockingCollectionT-Demonstration-in-Producer-Consumer-Scenarios#c633926133850000000</guid>
		<dc:creator>staceyw</dc:creator>
	</item>
	<item>
		<title>Re: BlockingCollection&amp;#60T&amp;#62 Demonstration in Producer-Consumer Scenarios</title>
		<description>
			<![CDATA[
<p>re: &quot;_ =&gt;&quot;</p>
<p>&nbsp;</p>
<p>An underscore is a valid identifier in C#.&nbsp; I sometimes use it when I don't care about the variable in question but still need to name it, so I name it with an underscore.</p>
<p>&nbsp;</p>
<p>re: TPL</p>
<p>&nbsp;</p>
<p>.NET 4.&nbsp; And Beta&nbsp;is available, and it has a go-live license associated with it, so I suggest you go download it today <img src='http://ecn.channel9.msdn.com/o9/content/images/emoticons/emotion-1.gif' alt='Smiley' /></p>
<p>posted by Toub</p>]]>
		</description>
		<link>http://channel9.msdn.com/Blogs/philpenn/BlockingCollectionT-Demonstration-in-Producer-Consumer-Scenarios#c633927236030000000</link>
		<pubDate>Mon, 02 Nov 2009 01:53:23 GMT</pubDate>
		<guid isPermaLink="true">http://channel9.msdn.com/Blogs/philpenn/BlockingCollectionT-Demonstration-in-Producer-Consumer-Scenarios#c633927236030000000</guid>
		<dc:creator>Toub</dc:creator>
	</item>
	<item>
		<title>Re: BlockingCollection&amp;#60T&amp;#62 Demonstration in Producer-Consumer Scenarios</title>
		<description>
			<![CDATA[
<p>gdesroches,</p>
<p>&nbsp;</p>
<p>Great question and something we actually thought of considering.&nbsp; As you've pointed out, there is more than one way to pass data from a consumer to a producer.&nbsp; If we think individually&nbsp;about the communication on either side of the data structure (P for&nbsp;production,
 C for consumption)&nbsp;as being synchronous or asynchronous we come up with four different styles: P(sync)/C(sync), P(async)/C(sync), P(sync)/C(async), and P(async)/C(async).&nbsp; I believe you're referring to the last one.&nbsp;
</p>
<p>&nbsp;</p>
<p>P(sync)/C(sync) is essentially a single item data structure that synchronizes the producer and consumer in lock step with each other.&nbsp; You can think of this as a&nbsp;two people&nbsp;in the middle of a bucket line trying to put out a fire.&nbsp; The person closer to the
 beginning of the line will not grab another bucket (more work) until the person next to them takes their bucket and the person closer to the end of the line will&nbsp;wait and not do any work until a bucket is available.&nbsp; This can be achieved in .NET 4 by creating
 a BlockingCollection&lt;T&gt; and bounding it with a capacity of 1.&nbsp; </p>
<p>&nbsp;</p>
<p>P(aync)/C(sync) allows the producer to drop off items as fast as possible asynchronously but the consumer will block until data is available.&nbsp; In our fire brigade example, this would be analogous to a single person throwing water on the fire and everyone
 else grabbing buckets of water, dropping them off near the thrower and returning to get more.&nbsp; This is the default mode supported by BlockingCollection&lt;T&gt;.&nbsp;
</p>
<p>&nbsp;</p>
<p>P(sync)/C(async) is all about throttling.&nbsp; Producers can only drop off as many items as their are available slots before being blocked but as soon as work&nbsp;is available, a consumer is created and given the data.&nbsp; Think of this as the previous example accept
 that there are a limited number of buckets and so the people grabbing the water must wait until a bucket is emptied before filling it.&nbsp;&nbsp;&nbsp;Instead of a single person&nbsp;throwing water, there is a pool of people sitting around and whenever a&nbsp;full bucket appears,
 one of them runs up, throws the water and returns to the&nbsp;pool.&nbsp;There isn't any built in functionality to support this in .NET 4.&nbsp; With your idea of an event, this could be supported on BlockingCollection&lt;T&gt;.</p>
<p>&nbsp;</p>
<p>P(async)/C(async) is the last model and the one I think you're most interested in.&nbsp; Producers can drop off as many items as they'd like and as soon as work is available, a consumer is created and given the data.&nbsp; This is the combination of an arbitrary amount
 of workers grabbing buckets and dropping them off and an arbitrary amount of workers being handed buckets as soon as buckets are available.&nbsp;&nbsp;This is the most scalable model and it is also the model that is supported by the CCR's Port types.</p>
<p>&nbsp;</p>
<p>That said, there are trade-offs to all of these models.&nbsp; Speaking strictly from the consumer side, ordering becomes a big issue.&nbsp; Blocking a thread allows you to easily maintain some state on a given thread and guarantee that you'll process messages in the
 order that they arrive (provided that the producer keeps the messages in order).&nbsp; This is absolutely essential for some streaming scenarios (like encryption) and simply cannot be maintained by completely asynchronous communication.&nbsp;
</p>
<p>&nbsp;</p>
<p>And so, after going off on a huge (but hopefully informative) tangent, in short, we realize there's a gap in functionality but didn't want to cram every scenario into a single, bloated&nbsp;type.&nbsp; We're hoping to fill some of these gaps in the next version of
 .NET but, until then, the CCR may be of benefit to you.</p>
<p>&nbsp;</p>
<p>Josh</p>
<p>posted by phillips.josh</p>]]>
		</description>
		<link>http://channel9.msdn.com/Blogs/philpenn/BlockingCollectionT-Demonstration-in-Producer-Consumer-Scenarios#c633928761260000000</link>
		<pubDate>Tue, 03 Nov 2009 20:15:26 GMT</pubDate>
		<guid isPermaLink="true">http://channel9.msdn.com/Blogs/philpenn/BlockingCollectionT-Demonstration-in-Producer-Consumer-Scenarios#c633928761260000000</guid>
		<dc:creator>phillips.josh</dc:creator>
	</item>
	<item>
		<title>Re: BlockingCollection&amp;#60T&amp;#62 Demonstration in Producer-Consumer Scenarios</title>
		<description>
			<![CDATA[
<p>It would be excellent if these 10 min videos were woven into the MSDN help content. It seems to me that they are great as documentation. It's very cool that every type can have a video presentation like this; a computational cast of characters.</p>
<p>posted by exoteric</p>]]>
		</description>
		<link>http://channel9.msdn.com/Blogs/philpenn/BlockingCollectionT-Demonstration-in-Producer-Consumer-Scenarios#c633929752390000000</link>
		<pubDate>Wed, 04 Nov 2009 23:47:19 GMT</pubDate>
		<guid isPermaLink="true">http://channel9.msdn.com/Blogs/philpenn/BlockingCollectionT-Demonstration-in-Producer-Consumer-Scenarios#c633929752390000000</guid>
		<dc:creator>exoteric</dc:creator>
	</item>
</channel>
</rss>