﻿<?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>Mark Knell commented on Code Review: Who Can Help Me</title><description>What am I missing?  The docs at msdn.microsoft.com/en-us/library/xfhwa508.aspx say "...where an enumeration contends with write accesses, the collection must be locked during the entire enumeration. To allow the collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization." Are the docs wrong?
  
  
If not, this code seems to fulfil their requirements.  (It can throw exceptions, but "threadsafe" doesn't mean "exception free". I'd assume that fail-fast is what we want.) The ContainsKey method will either get a true hit, a false miss, or a true miss.  A true hit is no problem.  Either kind of miss will enter the double-checked lock. Assuming we don't have a debugger attached (which can make "lock" theoretically non-thread-safe), the second ContainsKey evaluation will have its collection locked for the entire enumeration, per the docs. 
  
  
There could be a problem if you pass in an object that overrides Equals() in a sketchy way, such that it's not safe for Dictionary use.  If two different objects can appear equal, then they'll get the same synchronization object. But that sort of kludge is not "safe" for most uses anyhow.
</description><link>http://ayende.com/4812/code-review-who-can-help-me#comment18</link><guid>http://ayende.com/4812/code-review-who-can-help-me#comment18</guid><pubDate>Thu, 28 Apr 2011 20:58:09 GMT</pubDate></item><item><title>firefly commented on Code Review: Who Can Help Me</title><description>You are right... it's still breaking at the return... I was sloppy :)
</description><link>http://ayende.com/4812/code-review-who-can-help-me#comment17</link><guid>http://ayende.com/4812/code-review-who-can-help-me#comment17</guid><pubDate>Sat, 16 Apr 2011 04:51:30 GMT</pubDate></item><item><title>Ayende Rahien commented on Code Review: Who Can Help Me</title><description>James,
  
My problem with the mapping is that we can't really see what is going on. It is broken all over the place.
  
I would rather see it all in one place so that I can follow it.
</description><link>http://ayende.com/4812/code-review-who-can-help-me#comment16</link><guid>http://ayende.com/4812/code-review-who-can-help-me#comment16</guid><pubDate>Sat, 16 Apr 2011 04:43:26 GMT</pubDate></item><item><title>Ayende Rahien commented on Code Review: Who Can Help Me</title><description>firefly,
  
No, it cannot be made thread safe as written
</description><link>http://ayende.com/4812/code-review-who-can-help-me#comment15</link><guid>http://ayende.com/4812/code-review-who-can-help-me#comment15</guid><pubDate>Sat, 16 Apr 2011 04:41:41 GMT</pubDate></item><item><title>firefly commented on Code Review: Who Can Help Me</title><description>It will be thread safe if the dictionary is declared as volatile... still I have no idea what this class suppose to do.
</description><link>http://ayende.com/4812/code-review-who-can-help-me#comment14</link><guid>http://ayende.com/4812/code-review-who-can-help-me#comment14</guid><pubDate>Sat, 16 Apr 2011 04:27:46 GMT</pubDate></item><item><title>Harry Steinhilber commented on Code Review: Who Can Help Me</title><description>@RJ They are storing objects as keys in a static dictionary. Even if every other reference to the object goes out of scope, they dictionary will still hold a reference as a key, and the dictionary is never cleared out. So anything added to the dictionary, which is any object you call LockableObjectProvider.GetLockableObject() on, will never be garbage collected.
</description><link>http://ayende.com/4812/code-review-who-can-help-me#comment13</link><guid>http://ayende.com/4812/code-review-who-can-help-me#comment13</guid><pubDate>Fri, 15 Apr 2011 19:30:11 GMT</pubDate></item><item><title>Shane Courtrille commented on Code Review: Who Can Help Me</title><description>The one question I've got is how does one avoid the ever growing service with an implementation like this?  It seems like the NewsService could end up with quite a lot of code being so very procedural.  I'm not arguing for abstraction.. but a little OO would be nice no?
</description><link>http://ayende.com/4812/code-review-who-can-help-me#comment12</link><guid>http://ayende.com/4812/code-review-who-can-help-me#comment12</guid><pubDate>Fri, 15 Apr 2011 17:07:13 GMT</pubDate></item><item><title>James commented on Code Review: Who Can Help Me</title><description>"One thing that did bother me is the amount of mapping that is going on there, and how much of that I was simply unable to follow."
  
  
How would you prefer to handle the mapping?  Is your concern that the mapping for the SearchResultsPageViewModel doesn't happen in one place?
</description><link>http://ayende.com/4812/code-review-who-can-help-me#comment11</link><guid>http://ayende.com/4812/code-review-who-can-help-me#comment11</guid><pubDate>Fri, 15 Apr 2011 16:39:58 GMT</pubDate></item><item><title>RJ commented on Code Review: Who Can Help Me</title><description>Where is the memory leak?
</description><link>http://ayende.com/4812/code-review-who-can-help-me#comment10</link><guid>http://ayende.com/4812/code-review-who-can-help-me#comment10</guid><pubDate>Fri, 15 Apr 2011 15:04:54 GMT</pubDate></item><item><title>Rob Ashton commented on Code Review: Who Can Help Me</title><description>Damien :: In general. no - you shouldn't be locking the actual object under usage, because it's not fine grained enough and if other consumers are also doing similar you can end up with deadlocks occurring over unrelated functionality that shouldn't involve deadlocks.
  
  
That said, the above implementation ALSO has that problem - you should *really* perform locks on specific data for specific functionality, and not perform locks on exposed functional objects.
</description><link>http://ayende.com/4812/code-review-who-can-help-me#comment9</link><guid>http://ayende.com/4812/code-review-who-can-help-me#comment9</guid><pubDate>Fri, 15 Apr 2011 11:50:07 GMT</pubDate></item><item><title>Damien commented on Code Review: Who Can Help Me</title><description>public static object GetLockableObject(object o)
  
{
  
  return o;
  
}
  
  
surely?
</description><link>http://ayende.com/4812/code-review-who-can-help-me#comment8</link><guid>http://ayende.com/4812/code-review-who-can-help-me#comment8</guid><pubDate>Fri, 15 Apr 2011 11:03:52 GMT</pubDate></item><item><title>Steven Robbins commented on Code Review: Who Can Help Me</title><description>I'm probably taking this off topic (into implementation details of dictionary and its internal data structures) .. probably sufficient to say "it's calling methods from multiple threads that aren't guaranteed to be thread safe" and leave it at that :-)
</description><link>http://ayende.com/4812/code-review-who-can-help-me#comment7</link><guid>http://ayende.com/4812/code-review-who-can-help-me#comment7</guid><pubDate>Fri, 15 Apr 2011 09:58:11 GMT</pubDate></item><item><title>Ayende Rahien commented on Code Review: Who Can Help Me</title><description>Yes, on the face of it, this is safe code, in the sense that we will never get DuplicateKeyException. But the implementation of ContainsKey() isn’t safe to run when Add() is also executing.
  
  
The actual behavior depends on the implementation, but it is easy enough to imagine a scenario where invariants that ContainsKey() relies on are broken for the duration of the Add() call.
  
</description><link>http://ayende.com/4812/code-review-who-can-help-me#comment6</link><guid>http://ayende.com/4812/code-review-who-can-help-me#comment6</guid><pubDate>Fri, 15 Apr 2011 09:52:59 GMT</pubDate></item><item><title>Steven Robbins commented on Code Review: Who Can Help Me</title><description>@Patrick ah - it's a dictionary problem, fair enough, I was looking at the general structure rather than what it was actually doing. Thanks.
  
  
Out of interest, how does it "break" ? It should never (theoretically) be calling add and get concurrently if ContainsKey doesn't return true until the item is actually added, and not when it's halfway though adding it.
</description><link>http://ayende.com/4812/code-review-who-can-help-me#comment5</link><guid>http://ayende.com/4812/code-review-who-can-help-me#comment5</guid><pubDate>Fri, 15 Apr 2011 09:51:08 GMT</pubDate></item><item><title>Bogdan Marian commented on Code Review: Who Can Help Me</title><description>So, a ConcurrentDictionary (
[msdn.microsoft.com/en-us/library/dd287191.aspx](http://msdn.microsoft.com/en-us/library/dd287191.aspx)) could be used instead if the double-locking ...
</description><link>http://ayende.com/4812/code-review-who-can-help-me#comment4</link><guid>http://ayende.com/4812/code-review-who-can-help-me#comment4</guid><pubDate>Fri, 15 Apr 2011 09:44:27 GMT</pubDate></item><item><title>Patrick Huizinga commented on Code Review: Who Can Help Me</title><description>Oh, and the same goes for concurrently using Get and Add.
  
  
Although thinking about it, maybe ContainsKey won't break, but Get definitely can.
</description><link>http://ayende.com/4812/code-review-who-can-help-me#comment3</link><guid>http://ayende.com/4812/code-review-who-can-help-me#comment3</guid><pubDate>Fri, 15 Apr 2011 09:39:03 GMT</pubDate></item><item><title>Patrick Huizinga commented on Code Review: Who Can Help Me</title><description>@Steven Robbins
  
  
You can't use ContainsKey concurrently with Add. When Add changes the underlying structure, ContainsKey will break.
</description><link>http://ayende.com/4812/code-review-who-can-help-me#comment2</link><guid>http://ayende.com/4812/code-review-who-can-help-me#comment2</guid><pubDate>Fri, 15 Apr 2011 09:36:58 GMT</pubDate></item><item><title>Steven Robbins commented on Code Review: Who Can Help Me</title><description>As silly as LockableObjectProvider is (hard to believe the developer didn't spot the "memory leak" problem), I don't see why it's not thread safe? Looks like a standard double check lock - so won't work in Java land, but should work ok on the MS CLR.
  
  
Not that I'm advocating it, just curious :-)
</description><link>http://ayende.com/4812/code-review-who-can-help-me#comment1</link><guid>http://ayende.com/4812/code-review-who-can-help-me#comment1</guid><pubDate>Fri, 15 Apr 2011 09:22:08 GMT</pubDate></item></channel></rss>