<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" 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:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>Parallel Extension</title>
        <link>http://geekswithblogs.net/vitus/category/10817.aspx</link>
        <description>Parallel Extension</description>
        <language>ru-RU</language>
        <copyright>Vitus</copyright>
        <managingEditor>vitus@movaxbx.com</managingEditor>
        <generator>Subtext Version 0.0.0.0</generator>
        <item>
            <title>.NET 4: &amp;ldquo;Slim&amp;rdquo;-style performance boost!</title>
            <link>http://geekswithblogs.net/vitus/archive/2010/04/15/net-4-slim-style-performance-boost.aspx</link>
            <description>&lt;p&gt;RTM version of .NET 4 and Visual Studio 2010 is &lt;a href="http://www.microsoft.com/visualstudio/ru-ru/download"&gt;available&lt;/a&gt;, and now we can do some test with it.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://msdn.microsoft.com/ru-ru/concurrency/ee851578%28en-us%29.aspx"&gt;Parallel Extensions&lt;/a&gt; is one of the most valuable part of .NET 4.0. It’s a set of good tools for easily consuming multicore hardware power. And it also contains some “upgraded” sync primitives – Slim-version.&lt;/p&gt;
&lt;p&gt;For example, it include updated variant of widely known &lt;a href="http://msdn.microsoft.com/en-us/library/system.threading.manualresetevent.aspx"&gt;ManualResetEvent&lt;/a&gt;. For people, who don’t know about it: you can sync concurrency execution of some pieces of code with this sync primitive. Instance of ManualResetEvent can be in 2 states: signaled and non-signaled. Transition between it possible by Set() and Reset() methods call. Some shortly explanation:&lt;/p&gt;
&lt;table cellspacing="0" cellpadding="2" border="0" width="400"&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td width="152" valign="top"&gt;
            &lt;p align="center"&gt;&lt;strong&gt;Thread 1&lt;/strong&gt;&lt;/p&gt;
            &lt;/td&gt;
            &lt;td width="144" valign="top"&gt;
            &lt;p align="center"&gt;&lt;strong&gt;Thread&lt;/strong&gt;&lt;strong&gt; 2&lt;/strong&gt;&lt;/p&gt;
            &lt;/td&gt;
            &lt;td width="102" valign="top"&gt;
            &lt;p align="center"&gt;&lt;strong&gt;Time&lt;/strong&gt;&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td width="152" valign="top"&gt;mre.Reset();          &lt;br /&gt;
            mre.WaitOne();&lt;/td&gt;
            &lt;td width="144" valign="top"&gt;//code execution&lt;/td&gt;
            &lt;td width="102" valign="top"&gt;0&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td width="152" valign="top"&gt;//wating&lt;/td&gt;
            &lt;td width="144" valign="top"&gt;//code execution&lt;/td&gt;
            &lt;td width="102" valign="top"&gt;1&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td width="152" valign="top"&gt;//wating&lt;/td&gt;
            &lt;td width="144" valign="top"&gt;//code execution&lt;/td&gt;
            &lt;td width="102" valign="top"&gt;2&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td width="152" valign="top"&gt;//wating&lt;/td&gt;
            &lt;td width="144" valign="top"&gt;//code execution&lt;/td&gt;
            &lt;td width="102" valign="top"&gt;3&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td width="152" valign="top"&gt;//wating&lt;/td&gt;
            &lt;td width="144" valign="top"&gt;mre.Set();&lt;/td&gt;
            &lt;td width="131" valign="top"&gt;4&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td width="152" valign="top"&gt;//code execution&lt;/td&gt;
            &lt;td width="144" valign="top"&gt;//…&lt;/td&gt;
            &lt;td width="131" valign="top"&gt;5&lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Upgraded version of this primitive is &lt;a href="http://msdn.microsoft.com/en-us/library/system.threading.manualreseteventslim%28VS.100%29.aspx"&gt;ManualResetEventSlim&lt;/a&gt;. The idea in decreasing performance cost in case, when only 1 thread use it. Main concept in the “hybrid sync schema”, which can be done as following:&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;div id="codeSnippetWrapper"&gt;
&lt;div id="codeSnippet" style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;internal&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;sealed&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;class&lt;/span&gt; SimpleHybridLock : IDisposable&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: rgb(244, 244, 244); margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
{&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
    &lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; Int32 m_waiters = 0;&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: rgb(244, 244, 244); margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
    &lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; AutoResetEvent m_waiterLock = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; AutoResetEvent(&lt;span style="color: rgb(0, 0, 255);"&gt;false&lt;/span&gt;);&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
 &lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: rgb(244, 244, 244); margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
    &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; Enter()&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
    {&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: rgb(244, 244, 244); margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
        &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (Interlocked.Increment(&lt;span style="color: rgb(0, 0, 255);"&gt;ref&lt;/span&gt; m_waiters) == 1)&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
            &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt;;&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: rgb(244, 244, 244); margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
        m_waiterLock.WaitOne();&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
    }&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: rgb(244, 244, 244); margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
 &lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
    &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; Leave()&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: rgb(244, 244, 244); margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
    {&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
        &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (Interlocked.Decrement(&lt;span style="color: rgb(0, 0, 255);"&gt;ref&lt;/span&gt; m_waiters) == 0)&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: rgb(244, 244, 244); margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
            &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt;;&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
        m_waiterLock.Set();&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: rgb(244, 244, 244); margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
    }&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
 &lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: rgb(244, 244, 244); margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
    &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; Dispose()&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
    {&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: rgb(244, 244, 244); margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
        m_waiterLock.Dispose();&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
    }&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: rgb(244, 244, 244); margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
}&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;It’s a sample from Jeffry Richter’s book &lt;a href="http://www.amazon.com/CLR-via-Dev-Pro-Jeffrey-Richter/dp/0735627045/ref=sr_1_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1271301676&amp;amp;sr=1-1"&gt;“CLR via C#”, 3rd edition&lt;/a&gt;. Primitive SimpleHybridLock have two public methods: Enter() and Leave(). You can put your concurrency-critical code between calls of these methods, and it would executed in only one thread at the moment. Code is really simple: first thread, called Enter(), increase counter. Second thread also increase counter, and suspend while m_waiterLock is not signaled. So, if we don’t have concurrent access to our lock, “heavy” methods WaitOne() and Set() will not called. It’s can give some performance bonus.&lt;/p&gt;
&lt;p&gt;ManualResetEvent use the similar idea. Of course, it have more “smart” technics inside, like a checking of recursive calls, and so on. I want to know a real difference between classic ManualResetEvent realization, and new –Slim. I wrote a simple “benchmark”:&lt;/p&gt;
&lt;div id="codeSnippetWrapper"&gt;
&lt;div id="codeSnippet" style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;class&lt;/span&gt; Program&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: rgb(244, 244, 244); margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
{&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
    &lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; Main(&lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;[] args)&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: rgb(244, 244, 244); margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
    {&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
        ManualResetEventSlim mres = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; ManualResetEventSlim(&lt;span style="color: rgb(0, 0, 255);"&gt;false&lt;/span&gt;);&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: rgb(244, 244, 244); margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
        ManualResetEventSlim mres2 = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; ManualResetEventSlim(&lt;span style="color: rgb(0, 0, 255);"&gt;false&lt;/span&gt;);&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
 &lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: rgb(244, 244, 244); margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
        ManualResetEvent mre = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; ManualResetEvent(&lt;span style="color: rgb(0, 0, 255);"&gt;false&lt;/span&gt;);&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
 &lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: rgb(244, 244, 244); margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
        &lt;span style="color: rgb(0, 0, 255);"&gt;long&lt;/span&gt; total = 0;&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
        &lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; COUNT = 50;&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: rgb(244, 244, 244); margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
 &lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
        &lt;span style="color: rgb(0, 0, 255);"&gt;for&lt;/span&gt; (&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; i = 0; i &amp;lt; COUNT; i++)&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: rgb(244, 244, 244); margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
        {&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
            mres2.Reset();&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: rgb(244, 244, 244); margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
            Stopwatch sw = Stopwatch.StartNew();&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
 &lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: rgb(244, 244, 244); margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
            ThreadPool.QueueUserWorkItem((obj) =&amp;gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
            {&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: rgb(244, 244, 244); margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
                &lt;span style="color: rgb(0, 128, 0);"&gt;//Method(mres, true);&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
                Method2(mre, &lt;span style="color: rgb(0, 0, 255);"&gt;true&lt;/span&gt;);&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: rgb(244, 244, 244); margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
                mres2.Set();&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
            });&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: rgb(244, 244, 244); margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
            &lt;span style="color: rgb(0, 128, 0);"&gt;//Method(mres, false);&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
            Method2(mre, &lt;span style="color: rgb(0, 0, 255);"&gt;false&lt;/span&gt;);&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: rgb(244, 244, 244); margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
 &lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
            mres2.Wait();&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: rgb(244, 244, 244); margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
            sw.Stop();&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
 &lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: rgb(244, 244, 244); margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
            Console.WriteLine(&lt;span style="color: rgb(0, 96, 128);"&gt;"Pass {0}: {1} ms"&lt;/span&gt;, i, sw.ElapsedMilliseconds);&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
            total += sw.ElapsedMilliseconds;&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: rgb(244, 244, 244); margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
        }&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
 &lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: rgb(244, 244, 244); margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
        Console.WriteLine();&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
        Console.WriteLine(&lt;span style="color: rgb(0, 96, 128);"&gt;"==============================="&lt;/span&gt;);&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: rgb(244, 244, 244); margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
        Console.WriteLine(&lt;span style="color: rgb(0, 96, 128);"&gt;"Done in average="&lt;/span&gt; + total / (&lt;span style="color: rgb(0, 0, 255);"&gt;double&lt;/span&gt;)COUNT);&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
        Console.ReadLine();&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: rgb(244, 244, 244); margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
    }&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
 &lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: rgb(244, 244, 244); margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
    &lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; Method(ManualResetEventSlim mre, &lt;span style="color: rgb(0, 0, 255);"&gt;bool&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;value&lt;/span&gt;)&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
    {&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: rgb(244, 244, 244); margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
        &lt;span style="color: rgb(0, 0, 255);"&gt;for&lt;/span&gt; (&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; i = 0; i &amp;lt; 9000000; i++)&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
        {&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: rgb(244, 244, 244); margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
            &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (&lt;span style="color: rgb(0, 0, 255);"&gt;value&lt;/span&gt;)&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
            {&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: rgb(244, 244, 244); margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
                mre.Set();&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
            }&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: rgb(244, 244, 244); margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
            &lt;span style="color: rgb(0, 0, 255);"&gt;else&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
            {&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: rgb(244, 244, 244); margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
                mre.Reset();&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
            }&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: rgb(244, 244, 244); margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
        }&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
    }&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: rgb(244, 244, 244); margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
 &lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
    &lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; Method2(ManualResetEvent mre, &lt;span style="color: rgb(0, 0, 255);"&gt;bool&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;value&lt;/span&gt;)&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: rgb(244, 244, 244); margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
    {&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
        &lt;span style="color: rgb(0, 0, 255);"&gt;for&lt;/span&gt; (&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; i = 0; i &amp;lt; 9000000; i++)&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: rgb(244, 244, 244); margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
        {&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
            &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (&lt;span style="color: rgb(0, 0, 255);"&gt;value&lt;/span&gt;)&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: rgb(244, 244, 244); margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
            {&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
                mre.Set();&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: rgb(244, 244, 244); margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
            }&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
            &lt;span style="color: rgb(0, 0, 255);"&gt;else&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: rgb(244, 244, 244); margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
            {&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
                mre.Reset();&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: rgb(244, 244, 244); margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
            }&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
        }&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: rgb(244, 244, 244); margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
    }&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible;"&gt;
}&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;I use 2 concurrent thread (the main thread and one from thread pool) for setting and resetting ManualResetEvents, and try to run test COUNT times, and calculate average execution time. Here is the results (I get it on my dual core notebook with T7250 CPU and Windows 7 x64):&lt;/p&gt;
&lt;table cellspacing="0" cellpadding="2" border="0" width="402"&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td width="200" valign="top"&gt;
            &lt;p align="center"&gt;ManualResetEvent&lt;/p&gt;
            &lt;/td&gt;
            &lt;td width="200" valign="top"&gt;
            &lt;p align="center"&gt;ManualResetEvent&lt;strong&gt;Slim&lt;/strong&gt;&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td width="200" valign="top"&gt;&lt;img height="341" border="0" width="313" src="http://geekswithblogs.net/images/geekswithblogs_net/vitus/WindowsLiveWriter/1491f37d49.NET4Slimstyleperformanceboost_AD31/image9_3.png" alt="image9" title="image9" style="border-width: 0px; display: inline;" /&gt;&lt;/td&gt;
            &lt;td width="200" valign="top"&gt;&lt;img height="337" border="0" width="279" src="http://geekswithblogs.net/images/geekswithblogs_net/vitus/WindowsLiveWriter/1491f37d49.NET4Slimstyleperformanceboost_AD31/image4_3.png" alt="image4" title="image4" style="border-width: 0px; display: inline;" /&gt;&lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Difference is obvious and serious – in 10 times!&lt;/p&gt;
&lt;p&gt;So, I think preferable way is using ManualResetEventSlim, because not always on calling Set() and Reset() will be called “heavy” methods for working with Windows kernel-mode objects. It’s a small and nice improvement! ;)&lt;/p&gt; &lt;img src="http://geekswithblogs.net/vitus/aggbug/139276.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Vitus</dc:creator>
            <guid>http://geekswithblogs.net/vitus/archive/2010/04/15/net-4-slim-style-performance-boost.aspx</guid>
            <pubDate>Wed, 14 Apr 2010 20:28:24 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/vitus/comments/139276.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/vitus/archive/2010/04/15/net-4-slim-style-performance-boost.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/vitus/comments/commentRss/139276.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Thread-safe data structures .NET 4.0 (part 2)</title>
            <link>http://geekswithblogs.net/vitus/archive/2009/10/25/thread-safe-data-structures-.net-4.0-part-2.aspx</link>
            <description>&lt;p&gt;Today I tell you about BlockingCollection&amp;lt;T&amp;gt;. It’s more complex and more interesting data storage.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;BlockingCollection&amp;lt;T&amp;gt;&lt;/strong&gt; is a thread-safe data structure, which is called “blocking”, because it based on following principles:&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;if the collection is empty, and some code try to take an element, the thread, that executes this code, is blocked while any at least one element is not added;&lt;/li&gt;
    &lt;li&gt;if the collection already contains maximum elements, the thread, attempting to add new element, will be blocked until free space for the element is not available.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Of course, it’s not a full description of the collection’s possibilities.&lt;/p&gt;
&lt;p&gt;Let’s start to introduce with BlockingCollection&amp;lt;T&amp;gt;. It has few methods for manipulating data:&lt;/p&gt;
&lt;table cellspacing="0" cellpadding="0" border="1" width="640"&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td valign="top" width="102"&gt;
            &lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/p&gt;
            &lt;/td&gt;
            &lt;td valign="top" width="268"&gt;
            &lt;p align="center"&gt;&lt;strong&gt;Blocking methods&lt;/strong&gt;&lt;/p&gt;
            &lt;/td&gt;
            &lt;td valign="top" width="268"&gt;
            &lt;p align="center"&gt;&lt;strong&gt;non-Blocking methods&lt;/strong&gt;&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td valign="top" width="102"&gt;
            &lt;p align="center"&gt;&lt;strong&gt;Add&lt;/strong&gt;&lt;/p&gt;
            &lt;/td&gt;
            &lt;td valign="top" width="268"&gt;Add(T);          &lt;br /&gt;
            Add(T, CancellationToken); &lt;/td&gt;
            &lt;td valign="top" width="268"&gt;TryAdd(T)          &lt;br /&gt;
            TryAdd(T, Int32)           &lt;br /&gt;
            TryAdd(T, TimeSpan)           &lt;br /&gt;
            TryAdd(T, Int32, CancellationToken) &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td valign="top" width="102"&gt;
            &lt;p align="center"&gt;&lt;strong&gt;Take&lt;/strong&gt;&lt;/p&gt;
            &lt;/td&gt;
            &lt;td valign="top" width="268"&gt;Take()          &lt;br /&gt;
            Take(CancellationToken) &lt;/td&gt;
            &lt;td valign="top" width="268"&gt;TryTake(out T)          &lt;br /&gt;
            TryTake(out T, Int32)           &lt;br /&gt;
            TryTake(out T, TimeSpan)           &lt;br /&gt;
            TryTake(out T, Int32, CancellationToken) &lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;How you can see, we have both types of methods – blocking and non-blocking (starts with “Try” prefix). Try*-method return true on success of adding or taking an element, else – false. Both types have overloads with cancellation token, which allow to cancel method async.&lt;/p&gt;
&lt;p&gt;BlockingCollection&amp;lt;T&amp;gt; have mechanism of completion. Collection is completed, if we will not add any data in future. After completion all attempts to add some data will result in generation of InvalidOperationException. If we try to take an element from empty completed collection, we also have an exception. When we create a collection, it is not completed, and completion is doing by calling CompeteAdding() method. So, we must complete the collection explicitly. How you can use the completion mechanism? You can sync producer and consumer, i.e. parts of code, adding and taking data from the collection. Producer may notice, that new data will not be added by marking the collection as completed. So, consumer will not be wait for new elements.&lt;/p&gt;
&lt;p&gt;Ok, here is some code, that demonstrate how to work with BlockingCollection&amp;lt;T&amp;gt;. First of all we instantiate the collection, and set max elements count. However, we can use constructor overload without parameters, that allow collection to grow “infinite”:&lt;/p&gt;
&lt;div id="codeSnippetWrapper"&gt;
&lt;div id="codeSnippet" style="border-style: none; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: white; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// create the collection&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;BlockingCollection&amp;lt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&amp;gt; collection = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; BlockingCollection&amp;lt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&amp;gt;(10);&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Next we will add some elements and print some properties of collection (I’ll talk about it later):&lt;/p&gt;
&lt;div id="codeSnippetWrapper"&gt;
&lt;div id="codeSnippet" style="border-style: none; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: white; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;collection.Add(200);&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;collection.Add(300);&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: white; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;collection.Add(400);&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;collection.Add(500);&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: white; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt; &lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;Console.WriteLine(&lt;span style="color: rgb(0, 96, 128);"&gt;"Count: "&lt;/span&gt; + collection.Count);&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: white; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;Console.WriteLine(&lt;span style="color: rgb(0, 96, 128);"&gt;"BoundedCapacity: "&lt;/span&gt; + collection.BoundedCapacity);&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;Console.WriteLine(&lt;span style="color: rgb(0, 96, 128);"&gt;"IsCompleted: "&lt;/span&gt; + collection.IsCompleted);&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: white; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;Console.WriteLine(&lt;span style="color: rgb(0, 96, 128);"&gt;"IsAddingCompleted: "&lt;/span&gt; + collection.IsAddingCompleted);&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;Console.WriteLine();&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;After that we start the timer. At the callback delegate more elements will be added, and collection is mark as completed:&lt;/p&gt;
&lt;div id="codeSnippetWrapper"&gt;
&lt;div id="codeSnippet" style="border-style: none; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: white; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;Timer timer = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; Timer(&lt;span style="color: rgb(0, 0, 255);"&gt;delegate&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;{&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: white; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;    Console.ForegroundColor = ConsoleColor.Red;&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;    Console.WriteLine(DateTime.Now.ToLongTimeString() + &lt;span style="color: rgb(0, 96, 128);"&gt;" Adding "&lt;/span&gt; + &lt;span style="color: rgb(0, 96, 128);"&gt;" 600"&lt;/span&gt;);&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: white; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;    collection.Add(600);&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;    Thread.SpinWait(300000000);&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: white; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt; &lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;    Console.ForegroundColor = ConsoleColor.Red;&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: white; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;    Console.WriteLine(DateTime.Now.ToLongTimeString() + &lt;span style="color: rgb(0, 96, 128);"&gt;" Adding "&lt;/span&gt; + &lt;span style="color: rgb(0, 96, 128);"&gt;" 700"&lt;/span&gt;);&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;    collection.Add(700);&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: white; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt; &lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;    collection.CompleteAdding();&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: white; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;},&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;  &lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;, 3000, Timeout.Infinite);&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Timer callback will be executed in other thread, than the Main() method. It allow us to model concurrent access to the collection. Then we start taking elements from collection (I use different font colors in different threads):&lt;/p&gt;
&lt;div id="codeSnippetWrapper"&gt;
&lt;div id="codeSnippet" style="border-style: none; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: white; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;foreach&lt;/span&gt; (var item &lt;span style="color: rgb(0, 0, 255);"&gt;in&lt;/span&gt; collection.GetConsumingEnumerable())&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;{&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: white; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;    Console.ForegroundColor = ConsoleColor.Gray;&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;    Console.WriteLine(DateTime.Now.ToLongTimeString() + &lt;span style="color: rgb(0, 96, 128);"&gt;" Taking "&lt;/span&gt; + item);&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: white; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;}&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Here is the result of work:&lt;/p&gt;
&lt;p&gt; &lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/vitus/WindowsLiveWriter/Threadsafedatastructures.NET4.0part2_E949/image_7.png"&gt;&lt;img height="268" border="0" width="210" src="http://geekswithblogs.net/images/geekswithblogs_net/vitus/WindowsLiveWriter/Threadsafedatastructures.NET4.0part2_E949/image_thumb_2.png" alt="image" title="image" style="border: 0px none ; display: inline;" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;Some words on how this example is work. We create a collection, and add 4 element to it. Next we start the timer, so we have 2 threads:&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;the main thread, where method Main() is executing and taking elements from the collection;&lt;/li&gt;
    &lt;li&gt;the timer thread, where timer callback is executing and adding elements;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;On the screenshot you can see that first 4 elements took in time less than 1 second. After that main thread is blocked and waiting for new elements. Timer starts with 3 seconds delay, you can see it: element 500 was took in 40 sec. and element 600 was added in 43 sec. In the same 43 sec. main thread is unblocked and took newly added element 600. Next, the timer thread is executing SpinWait(), after that it add element 700 and complete the collection by calling &lt;em&gt;collection.CompleteAdding()&lt;/em&gt;. And the main thread is consuming this element and unblock, because the collection is completed and all elements are processed. If we comment CompleteAdding() call, the main thread will be blocked forever, because timer callback is returned and nobody mark collection as completed. And the main thread will wait for new elements infinite. So, the completion mechanism allows to avoid such situation and provide good way to solve the issue.&lt;/p&gt;
&lt;p&gt;Some words on collection’s properties. I think, the most interest of them are following:&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;&lt;strong&gt;IsAddingCompleted&lt;/strong&gt; – returns &lt;strong&gt;true&lt;/strong&gt;, if collection is marked as completed, else &lt;strong&gt;false&lt;/strong&gt;;&lt;/li&gt;
    &lt;li&gt;&lt;strong&gt;IsCompleted&lt;/strong&gt; – return &lt;strong&gt;true&lt;/strong&gt;, if collection is completed and empty, else &lt;strong&gt;false&lt;/strong&gt;;&lt;/li&gt;
    &lt;li&gt;&lt;strong&gt;BoundedCapacity&lt;/strong&gt; – return collection’s max capacity. Returns –1 in case of growing collection;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Take a look on the screenshot, and you can notice, when collection contains some elements, both of bool properties are false, because collection in not marked as completed. In the last moment it both are true, because all elements is taken and collection is completed.&lt;/p&gt;
&lt;p&gt;Ok, and what about order of elements in the collection? You may notice, that it work as FIFO queue. Is this a predefined behavior? Or we can change it? Answer to these questions give us more careful look at the constructor. It have overload, which contains IProducerConsumerCollection&amp;lt;T&amp;gt; parameter. It implement a storage for the elements. You can use one of the the following collections (described in the &lt;a href="http://geekswithblogs.net/vitus/archive/2009/10/24/thread-safe-data-structures-.net-4.0-part-1.aspx"&gt;part 1&lt;/a&gt;), included in the .NET 4 beta 2:&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;ConcurrentStack&amp;lt;T&amp;gt; – thread-safe stack; &lt;/li&gt;
    &lt;li&gt;ConcurrentQueue&amp;lt;T&amp;gt; – thread-safe queue; &lt;/li&gt;
    &lt;li&gt;ConcurrentBag&amp;lt;T&amp;gt; – thread-safe unordered collection, which allows duplicates;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So, BlockingCollection&amp;lt;T&amp;gt; is not a data storage. It implement a logic of access to the storage and mechanism of completion. By default it use ConcurrentQueue&amp;lt;T&amp;gt; as a storage, so you can see FIFO behavior at the samples. We can change it to stack in the constructor:&lt;/p&gt;
&lt;div id="codeSnippetWrapper"&gt;
&lt;div id="codeSnippet" style="border-style: none; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: white; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;BlockingCollection&amp;lt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&amp;gt; collection = &lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;                &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; BlockingCollection&amp;lt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&amp;gt;(&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; ConcurrentStack&amp;lt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&amp;gt;(), 10);&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;and we saw following result:&lt;/p&gt;
&lt;p&gt;&lt;img height="277" border="0" width="211" src="http://geekswithblogs.net/images/geekswithblogs_net/vitus/WindowsLiveWriter/Threadsafedatastructures.NET4.0part2_E949/image_10.png" alt="image" title="image" style="border: 0px none ; display: inline;" /&gt; &lt;/p&gt;
&lt;p&gt;How you can see, order of elements now conform to LIFO. You can realize custom data storage by implementing interface:&lt;/p&gt;
&lt;div id="codeSnippetWrapper"&gt;
&lt;div id="codeSnippet" style="border-style: none; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: white; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;interface&lt;/span&gt; IProducerConsumerCollection&amp;lt;T&amp;gt; : IEnumerable&amp;lt;T&amp;gt;, ICollection, IEnumerable&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;{&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: white; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; CopyTo(T[] array, &lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; index);&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;    T[] ToArray();&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: white; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;bool&lt;/span&gt; TryAdd(T item);&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;bool&lt;/span&gt; TryTake(&lt;span style="color: rgb(0, 0, 255);"&gt;out&lt;/span&gt; T item);&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: white; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;}&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;In addition, I want to talk about some interesting static methods of the class BlockingCollection&amp;lt;T&amp;gt;. You can interact with a number of collections simultaneously with the following set of static methods:&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;&lt;strong&gt;Blocking&lt;/strong&gt;
    &lt;ul&gt;
        &lt;li&gt;int BlockingCollection&amp;lt;T&amp;gt;.AddToAny(BlockingCollection&amp;lt;T&amp;gt;[], T); &lt;/li&gt;
        &lt;li&gt;int BlockingCollection&amp;lt;T&amp;gt;.TakeFromAny(BlockingCollection&amp;lt;T&amp;gt;[], out T); &lt;/li&gt;
    &lt;/ul&gt;
    &lt;/li&gt;
    &lt;li&gt;&lt;strong&gt;Non-Blocking&lt;/strong&gt;
    &lt;ul&gt;
        &lt;li&gt;int BlockingCollection&amp;lt;T&amp;gt;.TryAddToAny(BlockingCollection&amp;lt;T&amp;gt;[], T); &lt;/li&gt;
        &lt;li&gt;int BlockingCollection&amp;lt;T&amp;gt;.TryTakeFromAny(BlockingCollection&amp;lt;T&amp;gt;[], out T); &lt;/li&gt;
    &lt;/ul&gt;
    &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;All of these methods take an array of BlockingCollection&amp;lt;T&amp;gt; elements – it’s an array of collections, with it set you’ll interact. They are work as described early Add() and Take() methods, i.e. with blocking logic. Methods return index of the collection, where data has been added or taken, or –1 when the action attempt is fail. More detail description you can read in the &lt;a href="http://msdn.microsoft.com/en-us/library/dd287223%28VS.100%29.aspx"&gt;MSDN&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;And one more interesting fact. In the MSDN you can read, that, for example, TakeFromAny() method return –1, if the taking action is not successful. It’s a blocking method, and when you try to take an element from set of empty non-completed collections, it will wait infinite while an element will be added to any of collection. Or if you complete any of collection, it will throw an ArgumentException. So, you wait, or take an element, or take an exception. And I can’t understood, when TakeFromAny() can return –1. I create a &lt;a href="http://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/8848449a-b333-480a-a5bf-ca1b4b971b6f"&gt;topic&lt;/a&gt; on MSDN Parallel Extension forums, and &lt;a href="http://social.msdn.microsoft.com/Profile/en-US/?user=Reed%20Copsey%2c%20Jr.&amp;amp;referrer=http%3a%2f%2fsocial.msdn.microsoft.com%2fForums%2fen-US%2fparallelextensions%2fthread%2f8848449a-b333-480a-a5bf-ca1b4b971b6f&amp;amp;rh=m2YAT0%2fB4zbA7Z%2bsj3HtRsoBetsPpev5EO06Fn%2b4tzo%3d&amp;amp;sp=forums"&gt;Reed Copsey, Jr&lt;/a&gt; accept, that it’s just a documentation issue. He create an appropriate &lt;a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=498890"&gt;item&lt;/a&gt; on the MS Connect. So, you need to be careful when read current MSDN docs about thread-safe collections, because it’s just a preview and may contains an errors.&lt;/p&gt;
&lt;p&gt;In conclusion, other interesting samples of BlockingCollection&amp;lt;T&amp;gt; usage you can find at the &lt;a href="http://code.msdn.microsoft.com/ParExtSamples"&gt;MSDN code gallery&lt;/a&gt;. Now there are only samples for beta 1, but soon refreshed sample pack will be available. I advice you to download Visual &lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=dc333ac8-596d-41e3-ba6c-84264e761b81&amp;amp;displaylang=en"&gt;Studio 2010 beta 2&lt;/a&gt;, that is published this week. You can try to work with new features. It contains the latest Parallel Extensions library, that allow you to use full power of modern multi-core hardware. &lt;/p&gt; &lt;img src="http://geekswithblogs.net/vitus/aggbug/135700.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Vitus</dc:creator>
            <guid>http://geekswithblogs.net/vitus/archive/2009/10/25/thread-safe-data-structures-.net-4.0-part-2.aspx</guid>
            <pubDate>Sat, 24 Oct 2009 23:01:12 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/vitus/comments/135700.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/vitus/archive/2009/10/25/thread-safe-data-structures-.net-4.0-part-2.aspx#feedback</comments>
            <slash:comments>2</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/vitus/comments/commentRss/135700.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Thread-safe data structures .NET 4.0 (part 1)</title>
            <link>http://geekswithblogs.net/vitus/archive/2009/10/24/thread-safe-data-structures-.net-4.0-part-1.aspx</link>
            <description>&lt;p&gt;.NET 4 contains rich set of tools, that allow to create parallelized code more easy. But when we start processing chunk of data in parallel threads, we need to synchronize these threads, and we need some storage for the results of work. Now exist a big number of methods, solving sync issues, and we can realize in code any of them. But MS Parallel Extensions team already do it, and .NET 4 beta versions include set of thread-safe data structures. They implement some popular types of collections, and I want to do an overview of them.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;1. &lt;/strong&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/dd267265(VS.100).aspx"&gt;Queue&lt;/a&gt;&lt;strong&gt;: ConcurrentQueue&amp;lt;T&amp;gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This is a classic FIFO queue, which can be safely accessed from few threads simultaneously. API of this collection is very simple:&lt;/p&gt;
&lt;div id="codeSnippetWrapper"&gt;
&lt;div id="codeSnippet" style="border-style: none; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: white; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;ConcurrentQueue&amp;lt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&amp;gt; queue = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; ConcurrentQueue&amp;lt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&amp;gt;();&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;queue.Enqueue(10);&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: white; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt; &lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; t;&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: white; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;Console.WriteLine(queue.TryPeek(&lt;span style="color: rgb(0, 0, 255);"&gt;out&lt;/span&gt; t));&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;Console.WriteLine(queue.TryDequeue(&lt;span style="color: rgb(0, 0, 255);"&gt;out&lt;/span&gt; t));&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
TryPeek() and TryDequeue() methods return an element from queue, but TryPeek() doesn’t remove it from queue. Both methods returns &lt;strong&gt;true&lt;/strong&gt;, if they take an element, else they return &lt;strong&gt;false&lt;/strong&gt;. You can add an element by calling Enqueue() method. Count of elements in the queue you can get by Count property, and if collection contains at least 1 element, property IsEmpty returns true.&lt;/div&gt;
&lt;div&gt; &lt;/div&gt;
&lt;div&gt;&lt;strong&gt;2. &lt;/strong&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/dd267331%28VS.100%29.aspx"&gt;Stack&lt;/a&gt;&lt;strong&gt;: ConcurrentStack&amp;lt;T&amp;gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div&gt; &lt;/div&gt;
&lt;p&gt;This is a simple LIFO stack, that can be used in the concurrent environment. In contrast to queue, you can easily add or take the range of elements:&lt;/p&gt;
&lt;div id="codeSnippetWrapper"&gt;
&lt;div id="codeSnippet" style="border-style: none; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: white; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;ConcurrentStack&amp;lt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&amp;gt; stack = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; ConcurrentStack&amp;lt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&amp;gt;();&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;stack.Push(10);&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: white; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;stack.PushRange(&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;[] { 1, 2, 3, 4, 5 });&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt; &lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: white; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; t;&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (stack.TryPop(&lt;span style="color: rgb(0, 0, 255);"&gt;out&lt;/span&gt; t))&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: white; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;{&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;    Console.WriteLine(&lt;span style="color: rgb(0, 96, 128);"&gt;"Pop: "&lt;/span&gt; + t);&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: white; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;}&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (stack.TryPeek(&lt;span style="color: rgb(0, 0, 255);"&gt;out&lt;/span&gt; t))&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: white; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;{&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;    Console.WriteLine(&lt;span style="color: rgb(0, 96, 128);"&gt;"Peek: "&lt;/span&gt; + t);&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: white; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;}&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;[] ts = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;[5];&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: white; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; count;&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; ((count = stack.TryPopRange(ts, 0, 3)) &amp;gt; 0)&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: white; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;{&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;    Console.WriteLine(&lt;span style="color: rgb(0, 96, 128);"&gt;"PopRange"&lt;/span&gt;);&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: white; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;for&lt;/span&gt; (&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; i = 0; i &amp;lt; count; i++)&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;    {&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: white; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;        Console.WriteLine(ts[i]);&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;    }&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: white; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;}&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Here is the result of work of this code:&lt;/p&gt;
&lt;p&gt;&lt;img height="100" border="0" width="86" src="http://geekswithblogs.net/images/geekswithblogs_net/vitus/WindowsLiveWriter/Threadsafedatastructures.NET4.0part1_AA08/image_3.png" alt="image" title="image" style="border-width: 0px; display: inline;" /&gt; &lt;/p&gt;
&lt;p&gt;TryPeek() and TryPop() methods return bool values, and TryPopRange() – count of retrieved elements. You may add range of values by calling PushRange() method.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;3. &lt;/strong&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/dd381779%28VS.100%29.aspx"&gt;Collection&lt;/a&gt;&lt;strong&gt;: ConcurrentBag&amp;lt;T&amp;gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This is an unordered collection of data, which is looks like a set, but it can store duplicate items. It doesn’t guarantee any order of elements. I think, it’s a most simple data structure:&lt;/p&gt;
&lt;div id="codeSnippetWrapper"&gt;
&lt;div id="codeSnippet" style="border-style: none; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: white; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;ConcurrentBag&amp;lt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&amp;gt; bag = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; ConcurrentBag&amp;lt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;&amp;gt;(&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;[] { 1, 1, 2, 3 });&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;bag.Add(70);&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: white; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt; &lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; t;&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: white; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;bag.TryPeek(&lt;span style="color: rgb(0, 0, 255);"&gt;out&lt;/span&gt; t);&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;Console.WriteLine(t);&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: white; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt; &lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;bag.Add(110);&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: white; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt; &lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;Console.WriteLine();&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: white; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;for&lt;/span&gt; (&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; i = 0; i &amp;lt; 3; i++)&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;{&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: white; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;    bag.TryTake(&lt;span style="color: rgb(0, 0, 255);"&gt;out&lt;/span&gt; t);&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;    Console.WriteLine(t);&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: white; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;}&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;This piece of code produce following console output:&lt;/p&gt;
&lt;p&gt;&lt;img height="76" border="0" width="64" src="http://geekswithblogs.net/images/geekswithblogs_net/vitus/WindowsLiveWriter/Threadsafedatastructures.NET4.0part1_AA08/image_6.png" alt="image" title="image" style="border-width: 0px; display: inline;" /&gt; &lt;/p&gt;
&lt;p&gt;Looking at screenshot, you can notice, that elements retrieved in LIFO. I repeat: no any orders is guaranteed.&lt;/p&gt;
&lt;p&gt;Take a look at the constructor of bag – you can set initial range of elements, and constructors of queue and stack have appropriate overloads too.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;4. &lt;/strong&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/dd287191%28VS.100%29.aspx"&gt;Dictionary&lt;/a&gt;&lt;strong&gt;: ConcurrentDictionary&amp;lt;TKey, TValue&amp;gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;It’s just a variant of Dictionary&amp;lt;TKey, TValue&amp;gt;, which can be accessed concurrently. API for this collection has some thread-safe specific features:&lt;/p&gt;
&lt;div id="codeSnippetWrapper"&gt;
&lt;div id="codeSnippet" style="border-style: none; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: white; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;ConcurrentDictionary&amp;lt;&lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;, &lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;&amp;gt; dict = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; ConcurrentDictionary&amp;lt;&lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;, &lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;&amp;gt;();&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;dict.TryAdd(&lt;span style="color: rgb(0, 96, 128);"&gt;"name"&lt;/span&gt;, &lt;span style="color: rgb(0, 96, 128);"&gt;"OFC340"&lt;/span&gt;);&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: white; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;dict.TryAdd(&lt;span style="color: rgb(0, 96, 128);"&gt;"age"&lt;/span&gt;, &lt;span style="color: rgb(0, 96, 128);"&gt;"25"&lt;/span&gt;);&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;dict.TryAdd(&lt;span style="color: rgb(0, 96, 128);"&gt;"age"&lt;/span&gt;, &lt;span style="color: rgb(0, 96, 128);"&gt;"25"&lt;/span&gt;);&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Create/update/delete actions can be executed by calling one of Try* methods, which return &lt;strong&gt;true&lt;/strong&gt; on success, else &lt;strong&gt;false&lt;/strong&gt;. In the code sample, first attempt to add value “25” with key “age” is ok, and TryAdd return true. Second attempt is fail, and TryAdd() return false, and exception will not be generated. Other example:&lt;/p&gt;
&lt;div id="codeSnippetWrapper"&gt;
&lt;div id="codeSnippet" style="border-style: none; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: white; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt; t = &lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;.Empty;&lt;/pre&gt;
&lt;!--CRLF--&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;Console.WriteLine(dict.TryGetValue(&lt;span style="color: rgb(0, 96, 128);"&gt;"nokey"&lt;/span&gt;, &lt;span style="color: rgb(0, 0, 255);"&gt;out&lt;/span&gt; t));&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Key “nokey” is not present in the dictionary, but TryGetValue() method doesn’t generate any exception, and only return false. You can remove an element as shown here:&lt;/p&gt;
&lt;div id="codeSnippetWrapper"&gt;
&lt;div id="codeSnippet" style="border-style: none; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: white; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;Console.WriteLine(dict.TryRemove(&lt;span style="color: rgb(0, 96, 128);"&gt;"age"&lt;/span&gt;, &lt;span style="color: rgb(0, 0, 255);"&gt;out&lt;/span&gt; t));&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Using properties Values and Keys you can receive actual on the moment of call collections of values and keys of dictionary.&lt;/p&gt;
&lt;p&gt;So, you can see, that these data structures is very basic, and don’t contains tons of functions – only necessary things. I like this concept – it’s a base structures and too many functions is redundant. If you need something else, you can easily add it at any time.&lt;/p&gt;
&lt;p&gt;In addition to described data structures, .NET 4 beta 1 contains list ConcurrentLinkedList&amp;lt;T&amp;gt;. But I don’t cover it, because MSDN &lt;a href="http://msdn.microsoft.com/en-us/library/dd381935(VS.100).aspx"&gt;contains&lt;/a&gt; following warning: «&lt;em&gt;ConcurrentLinkedList(of T) is planned to be removed prior to the final release of Visual Studio 2010. Please&lt;/em&gt;&lt;em&gt; do not use this class&lt;/em&gt;». And in the .NET 4 beta 2 this collection is removed.&lt;/p&gt;
&lt;p&gt;Ok, I show you 4 basic thread-safe collections of data. In the next part of the article I show you BlockingCollection&amp;lt;T&amp;gt; – more complex and interest thread-safe storage. Stay tuned! :)&lt;/p&gt; &lt;img src="http://geekswithblogs.net/vitus/aggbug/135688.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Vitus</dc:creator>
            <guid>http://geekswithblogs.net/vitus/archive/2009/10/24/thread-safe-data-structures-.net-4.0-part-1.aspx</guid>
            <pubDate>Sat, 24 Oct 2009 04:20:00 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/vitus/comments/135688.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/vitus/archive/2009/10/24/thread-safe-data-structures-.net-4.0-part-1.aspx#feedback</comments>
            <slash:comments>21</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/vitus/comments/commentRss/135688.aspx</wfw:commentRss>
        </item>
    </channel>
</rss>
