{"id":55949,"date":"2010-01-26T13:14:00","date_gmt":"2010-01-26T13:14:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/pfxteam\/2010\/01\/26\/faq-are-all-of-the-new-concurrent-collections-lock-free\/"},"modified":"2010-01-26T13:14:00","modified_gmt":"2010-01-26T13:14:00","slug":"faq-are-all-of-the-new-concurrent-collections-lock-free","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet\/faq-are-all-of-the-new-concurrent-collections-lock-free\/","title":{"rendered":"FAQ :: Are all of the new concurrent collections lock-free?"},"content":{"rendered":"<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\">(This answer is based on the .NET Framework 4.<span>&nbsp; <\/span>As the details below are undocumented implementation details, they may change in future releases.)<\/p>\n<p><\/font><\/font><\/p>\n<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\">No.&nbsp; All of the collections in the new System.Collections.Concurrent namespace employ lock-free techniques to some extent in order to achieve general performance benefits, but traditional locks are used in some cases.<\/p>\n<p><\/font><\/font><\/p>\n<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\">It&rsquo;s worth noting that purely relying on lock-free techniques is sometimes not the most efficient solution.&nbsp; When we say &ldquo;lock-free,&rdquo; we mean that locks (in .NET, traditional mutual exclusion locks are available via the System.Threading.Monitor class, typically via the C# &ldquo;lock&rdquo; keyword or the Visual Basic &ldquo;SyncLock&rdquo; keyword) have been avoided by using memory barriers and compare-and-swap CPU instructions (in .NET, &ldquo;CAS&rdquo; operations are available via the System.Threading.Interlocked class).<\/p>\n<p><\/font><\/font><\/p>\n<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\">ConcurrentQueue&lt;T&gt; and ConcurrentStack&lt;T&gt; are completely lock-free in this way. They will never take a lock, but they may end up spinning and retrying an operation when faced with contention (when the CAS operations fail).<\/p>\n<p><\/font><\/font><\/p>\n<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\">ConcurrentBag&lt;T&gt; employs a multitude of mechanisms to minimize the need for synchronization. <span>&nbsp;<\/span>For example, it maintains a local queue for each thread that accesses it, and under some conditions, a thread is able to access its local queue in a lock-free manner with little or no contention. <span>&nbsp;<\/span>Therefore, while ConcurrentBag&lt;T&gt; sometimes requires locking, it is a very efficient collection for certain concurrent scenarios (e.g. many threads both producing and consuming at the same rate).<\/p>\n<p><\/font><\/font><\/p>\n<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\">ConcurrentDictionary&lt;TKey,TValue&gt; uses fine-grained locking when adding to or updating data in the dictionary, but it is entirely lock-free for read operations.<span>&nbsp; <\/span>In this way, it&rsquo;s optimized for scenarios where reading from the dictionary is the most frequent operation.<\/p>\n<p><\/font><\/font><\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>(This answer is based on the .NET Framework 4.&nbsp; As the details below are undocumented implementation details, they may change in future releases.) No.&nbsp; All of the collections in the new System.Collections.Concurrent namespace employ lock-free techniques to some extent in order to achieve general performance benefits, but traditional locks are used in some cases. It&rsquo;s [&hellip;]<\/p>\n","protected":false},"author":485,"featured_media":58792,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[7908],"tags":[7907,7916,7923],"class_list":["post-55949","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-pfxteam","tag-net-4","tag-coordination-data-structures","tag-faq"],"acf":[],"blog_post_summary":"<p>(This answer is based on the .NET Framework 4.&nbsp; As the details below are undocumented implementation details, they may change in future releases.) No.&nbsp; All of the collections in the new System.Collections.Concurrent namespace employ lock-free techniques to some extent in order to achieve general performance benefits, but traditional locks are used in some cases. It&rsquo;s [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/55949","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/users\/485"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/comments?post=55949"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/55949\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media\/58792"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media?parent=55949"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/categories?post=55949"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/tags?post=55949"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}