﻿<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>Ayende @ Rahien</title><link>http://ayende.com</link><description>Ayende @ Rahien</description><copyright>Copyright (C) Ayende Rahien  2004 - 2021 (c) 2026</copyright><ttl>60</ttl><item><title>Sony Mathew commented on Why is this not thread safe?</title><description>I missed the last fine-print of Ayende's entry.  Ok, I'll agree that in a bad implementation, thread B while executing its outer ContainsKey(o) enumerating can potentially break.
</description><link>http://ayende.com/4823/why-is-this-not-thread-safe#comment15</link><guid>http://ayende.com/4823/why-is-this-not-thread-safe#comment15</guid><pubDate>Thu, 05 May 2011 19:14:50 GMT</pubDate></item><item><title>Harry Steinhilber commented on Why is this not thread safe?</title><description>@Sony, 
  
No, it isn't thread safe. If thread A enters the lock and ContainsKey returns false, it will call Add. Now if in the middle of the Add (before it completes), thread B calls ContainsKey on the dictionary, the invariants of ContainsKey can be broken. As Ayende states, this is implementation dependent. But consider, what if Add had finished adding the key (so ContainsKey returns true) but not the value. At this point the indexer could return null or throw an exception if you try to access a key *you believe is already in the dictionary*. Hence, not thread safe.
</description><link>http://ayende.com/4823/why-is-this-not-thread-safe#comment14</link><guid>http://ayende.com/4823/why-is-this-not-thread-safe#comment14</guid><pubDate>Tue, 03 May 2011 00:34:45 GMT</pubDate></item><item><title>Sony Mathew commented on Why is this not thread safe?</title><description>This code is perfectly thread safe and the documentation supplied does not invalidate the thread safety of the code as I see it.  Once 'items' is locked successfully by a thread, no modifications can occur, your reads including enumerations and your if-check will be guaranteed.
</description><link>http://ayende.com/4823/why-is-this-not-thread-safe#comment13</link><guid>http://ayende.com/4823/why-is-this-not-thread-safe#comment13</guid><pubDate>Mon, 02 May 2011 18:07:23 GMT</pubDate></item><item><title>Simone commented on Why is this not thread safe?</title><description>ConcurrentDictionary is the solution, and has a great API too
</description><link>http://ayende.com/4823/why-is-this-not-thread-safe#comment12</link><guid>http://ayende.com/4823/why-is-this-not-thread-safe#comment12</guid><pubDate>Thu, 28 Apr 2011 20:47:48 GMT</pubDate></item><item><title>Dotan commented on Why is this not thread safe?</title><description>This is a pretty common pitfall. It might show itself as a livelock (very high or 100% CPU in production).
</description><link>http://ayende.com/4823/why-is-this-not-thread-safe#comment11</link><guid>http://ayende.com/4823/why-is-this-not-thread-safe#comment11</guid><pubDate>Wed, 27 Apr 2011 11:57:55 GMT</pubDate></item><item><title>billy commented on Why is this not thread safe?</title><description>@configurator you might want to consider that it is still several times slower to acquire and release a reader writer lock than a standard lock. If the thing you are doing inside the lock is fast, a simple lock should be fine.
  
  
You can test it out to see what performance gain you would actually get, my guess is hardly any.
</description><link>http://ayende.com/4823/why-is-this-not-thread-safe#comment10</link><guid>http://ayende.com/4823/why-is-this-not-thread-safe#comment10</guid><pubDate>Wed, 27 Apr 2011 11:55:54 GMT</pubDate></item><item><title>chibacity commented on Why is this not thread safe?</title><description>The example code is not thread-safe as the data structure is mutable, and an unlocked read operation is allowed. At any given time other thread(s) may be adding new items to the collection and therefore mutating it. If ContainsKey is executing whist the collection is being mutated, the outcome is indeterminate. For instance ContainsKey may depend on intermediate results as it executes which could become invalid as the collection is mutated by the addition of a new item e.g. the collection may have to be re-arranged to maintain the load factor when a new item is added. This could produce an incorrect result or an exception during the execution of ContainsKey.
</description><link>http://ayende.com/4823/why-is-this-not-thread-safe#comment9</link><guid>http://ayende.com/4823/why-is-this-not-thread-safe#comment9</guid><pubDate>Wed, 27 Apr 2011 08:29:28 GMT</pubDate></item><item><title>configurator commented on Why is this not thread safe?</title><description>A ReaderWriterLockSlim would be a good solution here.
  
  
ConcurrentDictionary is sometimes clunky,  it all depends on the rest of the using code; in this example, it would be easy to replace it with TryAdd, but if anyone else uses this dictionary they'd need to change their code. Of course, with a ReaderWriterLock they'd need to acquire an appropriate lock, but it's much easier to avoid issues that way sometimes.
</description><link>http://ayende.com/4823/why-is-this-not-thread-safe#comment8</link><guid>http://ayende.com/4823/why-is-this-not-thread-safe#comment8</guid><pubDate>Tue, 26 Apr 2011 18:16:50 GMT</pubDate></item><item><title>Erik Juhlin commented on Why is this not thread safe?</title><description>@Eber, no it's not. The ContainsKey call outside of the lock is not thread safe.
</description><link>http://ayende.com/4823/why-is-this-not-thread-safe#comment7</link><guid>http://ayende.com/4823/why-is-this-not-thread-safe#comment7</guid><pubDate>Tue, 26 Apr 2011 18:06:28 GMT</pubDate></item><item><title>Eber Irigoyen commented on Why is this not thread safe?</title><description>So this code *is* thread safe, the code that would not be thread safe would be the one enumerating the collection
</description><link>http://ayende.com/4823/why-is-this-not-thread-safe#comment6</link><guid>http://ayende.com/4823/why-is-this-not-thread-safe#comment6</guid><pubDate>Tue, 26 Apr 2011 14:41:08 GMT</pubDate></item><item><title>Jon Wingfield commented on Why is this not thread safe?</title><description>@Alex,  that's the most pessimistic approach though.
</description><link>http://ayende.com/4823/why-is-this-not-thread-safe#comment5</link><guid>http://ayende.com/4823/why-is-this-not-thread-safe#comment5</guid><pubDate>Tue, 26 Apr 2011 13:49:39 GMT</pubDate></item><item><title>Alex Simkin commented on Why is this not thread safe?</title><description>Just inner part works fine:
  
  
  lock(items)
  
    {
  
        if(items.ContainsKey(key) == false)
  
        {
  
            items.Add(key, val);
  
        }
  
    }
  
</description><link>http://ayende.com/4823/why-is-this-not-thread-safe#comment4</link><guid>http://ayende.com/4823/why-is-this-not-thread-safe#comment4</guid><pubDate>Tue, 26 Apr 2011 13:31:13 GMT</pubDate></item><item><title>Alexander Nyquist commented on Why is this not thread safe?</title><description>@John the "best" solution is to use ConcurrentDictionary. 
[msdn.microsoft.com/en-us/library/dd287191.aspx](http://msdn.microsoft.com/en-us/library/dd287191.aspx)</description><link>http://ayende.com/4823/why-is-this-not-thread-safe#comment3</link><guid>http://ayende.com/4823/why-is-this-not-thread-safe#comment3</guid><pubDate>Tue, 26 Apr 2011 13:19:42 GMT</pubDate></item><item><title>john wingfield  commented on Why is this not thread safe?</title><description>so what's the solution?  could you use a resetEvent to only block when writing?
</description><link>http://ayende.com/4823/why-is-this-not-thread-safe#comment2</link><guid>http://ayende.com/4823/why-is-this-not-thread-safe#comment2</guid><pubDate>Tue, 26 Apr 2011 13:09:41 GMT</pubDate></item><item><title>tobi commented on Why is this not thread safe?</title><description>For example during an array expansion.
</description><link>http://ayende.com/4823/why-is-this-not-thread-safe#comment1</link><guid>http://ayende.com/4823/why-is-this-not-thread-safe#comment1</guid><pubDate>Tue, 26 Apr 2011 09:20:48 GMT</pubDate></item></channel></rss>