<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://devplanet.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>What are we going to do tomorrow night, Brain?  - All Comments</title><link>http://devplanet.com/blogs/brianr/default.aspx</link><description /><dc:language>en</dc:language><generator>CommunityServer 2008.5 (Build: 30912.2823)</generator><item><title>I don&amp;#8217;t have the mental capacity to understand &amp;laquo;  Malaysian&amp;#039;s Chromosome</title><link>http://devplanet.com/blogs/brianr/archive/2008/09/26/thread-safe-dictionary-in-net.aspx#15992</link><pubDate>Fri, 05 Mar 2010 08:56:30 GMT</pubDate><guid isPermaLink="false">6e120f4b-f509-4111-8fd8-03bc0d0a75d9:15992</guid><dc:creator>I don’t have the mental capacity to understand «  Malaysian's Chromosome</dc:creator><description>&lt;p&gt;Pingback from &amp;nbsp;I don&amp;#8217;t have the mental capacity to understand &amp;laquo; &amp;nbsp;Malaysian&amp;#039;s Chromosome&lt;/p&gt;
&lt;img src="http://devplanet.com/aggbug.aspx?PostID=15992" width="1" height="1"&gt;</description></item><item><title>WPF for the Web guy</title><link>http://devplanet.com/blogs/brianr/archive/2008/09/26/thread-safe-dictionary-in-net.aspx#15990</link><pubDate>Wed, 20 Jan 2010 21:01:59 GMT</pubDate><guid isPermaLink="false">6e120f4b-f509-4111-8fd8-03bc0d0a75d9:15990</guid><dc:creator>mostlylucid</dc:creator><description>&lt;p&gt;WPF for the Web guy&lt;/p&gt;
&lt;img src="http://devplanet.com/aggbug.aspx?PostID=15990" width="1" height="1"&gt;</description></item><item><title>Twitter Trackbacks for                 Thread Safe Dictionary in .NET with ReaderWriterLockSlim - What are we going to do tomorrow night, Brain?         [dotnetcommandos.com]        on Topsy.com</title><link>http://devplanet.com/blogs/brianr/archive/2008/09/26/thread-safe-dictionary-in-net.aspx#15989</link><pubDate>Thu, 07 Jan 2010 21:46:33 GMT</pubDate><guid isPermaLink="false">6e120f4b-f509-4111-8fd8-03bc0d0a75d9:15989</guid><dc:creator>Twitter Trackbacks for                 Thread Safe Dictionary in .NET with ReaderWriterLockSlim - What are we going to do tomorrow night, Brain?         [dotnetcommandos.com]        on Topsy.com</dc:creator><description>&lt;p&gt;Pingback from &amp;nbsp;Twitter Trackbacks for &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Thread Safe Dictionary in .NET with ReaderWriterLockSlim - What are we going to do tomorrow night, Brain? &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; [dotnetcommandos.com] &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;on Topsy.com&lt;/p&gt;
&lt;img src="http://devplanet.com/aggbug.aspx?PostID=15989" width="1" height="1"&gt;</description></item><item><title>re: Thread Safe Dictionary Update</title><link>http://devplanet.com/blogs/brianr/archive/2008/09/29/thread-safe-dictionary-update.aspx#15988</link><pubDate>Wed, 21 Oct 2009 22:05:49 GMT</pubDate><guid isPermaLink="false">6e120f4b-f509-4111-8fd8-03bc0d0a75d9:15988</guid><dc:creator>dj_axl</dc:creator><description>&lt;p&gt;And to add to PeterZacho&amp;#39;s post...&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public ThreadSafeDictionary(IDictionary&amp;lt;TKey, TValue&amp;gt; dic)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;using (new WriteLock(this.dictionaryLock))&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.dict = new Dictionary&amp;lt;TKey, TValue&amp;gt;(dic);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devplanet.com/aggbug.aspx?PostID=15988" width="1" height="1"&gt;</description></item><item><title>re: Thread Safe Dictionary Update</title><link>http://devplanet.com/blogs/brianr/archive/2008/09/29/thread-safe-dictionary-update.aspx#15987</link><pubDate>Wed, 21 Oct 2009 20:21:48 GMT</pubDate><guid isPermaLink="false">6e120f4b-f509-4111-8fd8-03bc0d0a75d9:15987</guid><dc:creator>dj_axl</dc:creator><description>&lt;p&gt;For my use needed to add a cast...&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public static implicit operator Dictionary&amp;lt;TKey, TValue&amp;gt;(ThreadSafeDictionary&amp;lt;TKey, TValue&amp;gt; tsd)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;using (new ReadOnlyLock(tsd.dictionaryLock))&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return (Dictionary&amp;lt;TKey, TValue&amp;gt;)tsd.dict;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devplanet.com/aggbug.aspx?PostID=15987" width="1" height="1"&gt;</description></item><item><title>蛙蛙推荐：设计一个高效的网络服务器用户管理类</title><link>http://devplanet.com/blogs/brianr/archive/2008/09/26/thread-safe-dictionary-in-net.aspx#15683</link><pubDate>Fri, 10 Apr 2009 16:39:10 GMT</pubDate><guid isPermaLink="false">6e120f4b-f509-4111-8fd8-03bc0d0a75d9:15683</guid><dc:creator>蛙蛙池塘</dc:creator><description>&lt;p&gt;摘要：做一个有状态的网络服务端应用，一般需要维护一个在线用户列表，每次用户登录、注销都要修改这个列表，还得考虑超时清理的逻辑，对这个列表的操作大多时候需要用锁来进行线程同步，我们试图来用一种不需要线程同步的方法来做到这些事情。&lt;/p&gt;
&lt;img src="http://devplanet.com/aggbug.aspx?PostID=15683" width="1" height="1"&gt;</description></item><item><title>re: Thread Safe Dictionary Update</title><link>http://devplanet.com/blogs/brianr/archive/2008/09/29/thread-safe-dictionary-update.aspx#15463</link><pubDate>Mon, 23 Mar 2009 14:45:11 GMT</pubDate><guid isPermaLink="false">6e120f4b-f509-4111-8fd8-03bc0d0a75d9:15463</guid><dc:creator>Brian Rudolph</dc:creator><description>&lt;p&gt;I have done some scale testing in a few isolated situations. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;In general, this solution is at least as fast as one using monitor with a small number of threads. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;When I run it in some larger environments, like our inbound or outbound xml feed processors and caching models, where we have lots of threads, this model easily wins. &amp;nbsp;I have seen read\write performance increases of 80%+ when we have a dozen or more threads working concurrently. &amp;nbsp;These are situations where our servers would sit reasonably idle due to read\write contention. &amp;nbsp;Now we can easily spin them up to 95%+ active. &lt;/p&gt;
&lt;p&gt;As always, individual testing is important.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devplanet.com/aggbug.aspx?PostID=15463" width="1" height="1"&gt;</description></item><item><title>re: Thread Safe Dictionary Update</title><link>http://devplanet.com/blogs/brianr/archive/2008/09/29/thread-safe-dictionary-update.aspx#15430</link><pubDate>Thu, 19 Mar 2009 12:24:33 GMT</pubDate><guid isPermaLink="false">6e120f4b-f509-4111-8fd8-03bc0d0a75d9:15430</guid><dc:creator>Alpha</dc:creator><description>&lt;p&gt;Have you done any performance/scalability tests of this?&lt;/p&gt;
&lt;p&gt;When I look at the code it seems that the only methods that gain anything compared to a solution that just use a Monitor are CopyTo, Values and Keys (this is the methods that actually may hold the read lock for long periods) - and only if the dictionary contains a significant number of elements.&lt;/p&gt;
&lt;p&gt;The reason is that acquiring a ReaderWriteLockSlim is about 50% more expensive than acquiring a Monitor. In addition a simple dictionary operation (e.g. TryLookup, Add, and Remove) only cost about 25-50% of acquiring a Monitor.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devplanet.com/aggbug.aspx?PostID=15430" width="1" height="1"&gt;</description></item><item><title>MSCS Clusters, iSCSI, Distance Clusters and SQL 2005</title><link>http://devplanet.com/blogs/brianr/archive/2008/01/14/ms-dtc-configuration-for-sql-2005-in-a-distance-cluster-on-windows-server-2003-datacenter.aspx#15086</link><pubDate>Sat, 07 Mar 2009 05:51:22 GMT</pubDate><guid isPermaLink="false">6e120f4b-f509-4111-8fd8-03bc0d0a75d9:15086</guid><dc:creator>RTFM. Especially if you can't figure it out on your own!</dc:creator><description>&lt;p&gt;This has been a fun week with clustering. Myself and a few others have been working this week on setting&lt;/p&gt;
&lt;img src="http://devplanet.com/aggbug.aspx?PostID=15086" width="1" height="1"&gt;</description></item><item><title>re: Thread Safe Dictionary Update</title><link>http://devplanet.com/blogs/brianr/archive/2008/09/29/thread-safe-dictionary-update.aspx#14910</link><pubDate>Sat, 14 Feb 2009 22:02:03 GMT</pubDate><guid isPermaLink="false">6e120f4b-f509-4111-8fd8-03bc0d0a75d9:14910</guid><dc:creator>Brian Rudolph</dc:creator><description>&lt;p&gt;The problem with what you say about acquiring the read lock, and then releasing it before acquiring the write lock is simply because it opens the door for someone to insert the same value you are trying to insert before you acquire the write lock. &amp;nbsp;This would cause a duplicate key conflict and subsequent error. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;This is exactly the scenario that upgradeable read locks were designed for. &amp;nbsp;That is because noone can acquire a write lock before i&amp;#39;ve released my read lock. &amp;nbsp;Also, only one upgradeable lock can be held at a time, which prevents two threads waiting for a write lock while already holding an upgradeable read.&lt;/p&gt;
&lt;p&gt;I believe this is a fundamentally important feature in readerwriterlockslim.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devplanet.com/aggbug.aspx?PostID=14910" width="1" height="1"&gt;</description></item><item><title>re: Thread Safe Dictionary Update</title><link>http://devplanet.com/blogs/brianr/archive/2008/09/29/thread-safe-dictionary-update.aspx#14199</link><pubDate>Fri, 02 Jan 2009 11:15:55 GMT</pubDate><guid isPermaLink="false">6e120f4b-f509-4111-8fd8-03bc0d0a75d9:14199</guid><dc:creator>FrankB</dc:creator><description>&lt;p&gt;Hi Brain, can you explain why you are dooing a Busy-wait when acquiring a lock (the while loops)? This seems to be quite expensive.&lt;/p&gt;
&lt;p&gt;I also noticed that you used the upgradable read lock when dooing double check locking. While this is better than directly getting a Write lock, I would do the first check with just a read lock, release the read lock, and then acquire the write lock to do the update. This allows for better concurrency without casing deadlocks. &lt;/p&gt;
&lt;p&gt;You might also want to check out what microsoft is doing in PFX, I wrote a blog about their ConcurrentDictionay last week &lt;a rel="nofollow" target="_new" href="http://blogs.infosupport.com/blogs/frankb/archive/2008/12/31/Implementing-a-Thread-Safe-cache-using-the-Parallel-Extensions.aspx"&gt;blogs.infosupport.com/.../Implementing-a-Thread-Safe-cache-using-the-Parallel-Extensions.aspx&lt;/a&gt;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devplanet.com/aggbug.aspx?PostID=14199" width="1" height="1"&gt;</description></item><item><title>re: Thread Safe Dictionary Update</title><link>http://devplanet.com/blogs/brianr/archive/2008/09/29/thread-safe-dictionary-update.aspx#14158</link><pubDate>Sun, 28 Dec 2008 07:21:16 GMT</pubDate><guid isPermaLink="false">6e120f4b-f509-4111-8fd8-03bc0d0a75d9:14158</guid><dc:creator>PeterZacho</dc:creator><description>&lt;p&gt;Thanks for the code, Brian. While using it I realized that a couple of constructors are missing - the following changes helped me:&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;//This is the internal dictionary that we are wrapping&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;IDictionary&amp;lt;TKey, TValue&amp;gt; dict; &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;[NonSerialized]&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;ReaderWriterLockSlim dictionaryLock = Locks.GetLockInstance(LockRecursionPolicy.NoRecursion); //setup the lock;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public ThreadSafeDictionary() &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;dict = new Dictionary&amp;lt;TKey, TValue&amp;gt;();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public ThreadSafeDictionary(int capacity)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;dict = new Dictionary&amp;lt;TKey, TValue&amp;gt;(capacity);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public ThreadSafeDictionary(ThreadSafeDictionary&amp;lt;TKey, TValue&amp;gt; original)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;dict = new Dictionary&amp;lt;TKey, TValue&amp;gt;(original.dict);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devplanet.com/aggbug.aspx?PostID=14158" width="1" height="1"&gt;</description></item><item><title>re: Thread Safe Dictionary Update</title><link>http://devplanet.com/blogs/brianr/archive/2008/09/29/thread-safe-dictionary-update.aspx#13615</link><pubDate>Wed, 26 Nov 2008 19:47:23 GMT</pubDate><guid isPermaLink="false">6e120f4b-f509-4111-8fd8-03bc0d0a75d9:13615</guid><dc:creator>Brian Rudolph</dc:creator><description>&lt;p&gt;Interesting observations. The recursion issue i had already dealt with but had not posted. &amp;nbsp;The Serialization issue I had not run across. &amp;nbsp;I failed to recognize that the default constructor would not be called on binary deserialization.&lt;/p&gt;
&lt;p&gt;I have posted the updates. &amp;nbsp;Thanks for the feedback.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devplanet.com/aggbug.aspx?PostID=13615" width="1" height="1"&gt;</description></item><item><title>re: Thread Safe Dictionary Update</title><link>http://devplanet.com/blogs/brianr/archive/2008/09/29/thread-safe-dictionary-update.aspx#12940</link><pubDate>Thu, 20 Nov 2008 19:45:46 GMT</pubDate><guid isPermaLink="false">6e120f4b-f509-4111-8fd8-03bc0d0a75d9:12940</guid><dc:creator>mnero0429</dc:creator><description>&lt;p&gt;There&amp;#39;s a problem with this implementation in that it claims that it is serializable. &amp;nbsp;If the object is serialized, it looses its ReaderWriterLockSlim instance once it is deserialized. &amp;nbsp;Upon deserialization, you&amp;#39;ll start to encounter NullReferenceExcpetions when you call methods.&lt;/p&gt;
&lt;p&gt;Also, do you have a test instance that requires the ReaderWriterLockSlim instance to support lock recursion? &amp;nbsp;I couldn&amp;#39;t find any reason for this in your implementation of ThreadSafeDictionary.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devplanet.com/aggbug.aspx?PostID=12940" width="1" height="1"&gt;</description></item><item><title>re: Thread Safe Dictionary Update</title><link>http://devplanet.com/blogs/brianr/archive/2008/09/29/thread-safe-dictionary-update.aspx#14107</link><pubDate>Mon, 03 Nov 2008 19:17:18 GMT</pubDate><guid isPermaLink="false">6e120f4b-f509-4111-8fd8-03bc0d0a75d9:14107</guid><dc:creator>David Justice</dc:creator><description>&lt;p&gt;&amp;lt;a href=&amp;quot;&lt;a rel="nofollow" target="_new" href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fdevplanet.com%2fblogs%2fbrianr%2farchive%2f2008%2f09%2f29%2fthread-safe-dictionary-update.aspx&amp;quot;&amp;gt;&amp;lt;img"&gt;www.dotnetkicks.com/kick&lt;/a&gt; src=&amp;quot;&lt;a rel="nofollow" target="_new" href="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fdevplanet.com%2fblogs%2fbrianr%2farchive%2f2008%2f09%2f29%2fthread-safe-dictionary-update.aspx&amp;quot;"&gt;www.dotnetkicks.com/.../KickItImageGenerator.ashx&lt;/a&gt; border=&amp;quot;0&amp;quot; alt=&amp;quot;kick it on DotNetKicks.com&amp;quot; /&amp;gt;&amp;lt;/a&amp;gt;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devplanet.com/aggbug.aspx?PostID=14107" width="1" height="1"&gt;</description></item></channel></rss>