﻿<?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>Gian Maria Ricci commented on ConcurrentDIctionary.GetOrAdd may call the valueFactory method more than once</title><description>It would be nice if the documentation of GetOrAdd contains a remark telling you that the factory function could be called more than one times. I agree that we should never assume something :), but this violates the Principle of least astonishment.
  
  
Thanks Ayende for pointed this out.
</description><link>http://ayende.com/4802/concurrentdictionary-getoradd-may-call-the-valuefactory-method-more-than-once#comment11</link><guid>http://ayende.com/4802/concurrentdictionary-getoradd-may-call-the-valuefactory-method-more-than-once#comment11</guid><pubDate>Wed, 06 Apr 2011 07:24:01 GMT</pubDate></item><item><title>Ara commented on ConcurrentDIctionary.GetOrAdd may call the valueFactory method more than once</title><description>I meant, ConcurrentDictionary[TKey, Lazy[TValue] ]
</description><link>http://ayende.com/4802/concurrentdictionary-getoradd-may-call-the-valuefactory-method-more-than-once#comment10</link><guid>http://ayende.com/4802/concurrentdictionary-getoradd-may-call-the-valuefactory-method-more-than-once#comment10</guid><pubDate>Tue, 05 Apr 2011 17:57:24 GMT</pubDate></item><item><title>Ara commented on ConcurrentDIctionary.GetOrAdd may call the valueFactory method more than once</title><description>Use ConcurrentDictionary
&lt;tkey,&gt;
&gt;
  
  
When this happens, two instances of Lazy
&lt;tvalue will be created... instead of two instances of your value type.
&gt;</description><link>http://ayende.com/4802/concurrentdictionary-getoradd-may-call-the-valuefactory-method-more-than-once#comment9</link><guid>http://ayende.com/4802/concurrentdictionary-getoradd-may-call-the-valuefactory-method-more-than-once#comment9</guid><pubDate>Tue, 05 Apr 2011 17:56:11 GMT</pubDate></item><item><title>Nick Sergeev commented on ConcurrentDIctionary.GetOrAdd may call the valueFactory method more than once</title><description>WTF?!
  
I think MS should remove this method from ConcurrentDictionary.
</description><link>http://ayende.com/4802/concurrentdictionary-getoradd-may-call-the-valuefactory-method-more-than-once#comment8</link><guid>http://ayende.com/4802/concurrentdictionary-getoradd-may-call-the-valuefactory-method-more-than-once#comment8</guid><pubDate>Tue, 05 Apr 2011 17:06:37 GMT</pubDate></item><item><title>Frank Quednau commented on ConcurrentDIctionary.GetOrAdd may call the valueFactory method more than once</title><description>Oh, Benny Hill, awesome :)
  
  
That is a nasty one to remember.
  
  
Peter, ayende was referring to the implementation of ConcurrentDictionary
  
</description><link>http://ayende.com/4802/concurrentdictionary-getoradd-may-call-the-valuefactory-method-more-than-once#comment7</link><guid>http://ayende.com/4802/concurrentdictionary-getoradd-may-call-the-valuefactory-method-more-than-once#comment7</guid><pubDate>Tue, 05 Apr 2011 17:03:43 GMT</pubDate></item><item><title>Peter Morris commented on ConcurrentDIctionary.GetOrAdd may call the valueFactory method more than once</title><description>I don't know what you mean by "There are no locks" - WaitOne() is a lock isn't it, or do you mean something else?
</description><link>http://ayende.com/4802/concurrentdictionary-getoradd-may-call-the-valuefactory-method-more-than-once#comment6</link><guid>http://ayende.com/4802/concurrentdictionary-getoradd-may-call-the-valuefactory-method-more-than-once#comment6</guid><pubDate>Tue, 05 Apr 2011 15:11:47 GMT</pubDate></item><item><title>tobi commented on ConcurrentDIctionary.GetOrAdd may call the valueFactory method more than once</title><description>The reason for this is that user code should not be called while holding a lock (deadlock-prone. who knows if you function will call across threads and deadlock with itself?). Therefore the CDict will release the lock before calling your delegate. (They are internally using normal locks, but striped by hash code).
</description><link>http://ayende.com/4802/concurrentdictionary-getoradd-may-call-the-valuefactory-method-more-than-once#comment5</link><guid>http://ayende.com/4802/concurrentdictionary-getoradd-may-call-the-valuefactory-method-more-than-once#comment5</guid><pubDate>Tue, 05 Apr 2011 12:57:38 GMT</pubDate></item><item><title>Konstantin commented on ConcurrentDIctionary.GetOrAdd may call the valueFactory method more than once</title><description>There is a concept of "publish once" and "execute once" in Lazy
&lt;t, described here - 
[msdn.microsoft.com/.../...azythreadsafetymode.aspx](http://msdn.microsoft.com/en-us/library/system.threading.lazythreadsafetymode.aspx)&gt;  
  
If you only need "publish once" guarantee, framework can make more performance optimizations. Unfortunately this does not seem to have been documented for ConfurrentDictionary
</description><link>http://ayende.com/4802/concurrentdictionary-getoradd-may-call-the-valuefactory-method-more-than-once#comment4</link><guid>http://ayende.com/4802/concurrentdictionary-getoradd-may-call-the-valuefactory-method-more-than-once#comment4</guid><pubDate>Tue, 05 Apr 2011 12:28:06 GMT</pubDate></item><item><title>Ayende Rahien commented on ConcurrentDIctionary.GetOrAdd may call the valueFactory method more than once</title><description>Linkgoron,
  
To make sure that this actually run on separate threads.
</description><link>http://ayende.com/4802/concurrentdictionary-getoradd-may-call-the-valuefactory-method-more-than-once#comment3</link><guid>http://ayende.com/4802/concurrentdictionary-getoradd-may-call-the-valuefactory-method-more-than-once#comment3</guid><pubDate>Tue, 05 Apr 2011 10:58:50 GMT</pubDate></item><item><title>Linkgoron commented on ConcurrentDIctionary.GetOrAdd may call the valueFactory method more than once</title><description>Ayende, they're not totally lock free... But the delegate executes before the lock.
  
  
BTW, is there any reason you're using Threads instead of Tasks/TPL, or is just for demonstration?
</description><link>http://ayende.com/4802/concurrentdictionary-getoradd-may-call-the-valuefactory-method-more-than-once#comment2</link><guid>http://ayende.com/4802/concurrentdictionary-getoradd-may-call-the-valuefactory-method-more-than-once#comment2</guid><pubDate>Tue, 05 Apr 2011 10:56:33 GMT</pubDate></item><item><title>Michael Hawksworth commented on ConcurrentDIctionary.GetOrAdd may call the valueFactory method more than once</title><description>I always try to remember...
  
  
[http://www.youtube.com/watch?v=6hrLj8QEAgI](http://www.youtube.com/watch?v=6hrLj8QEAgI)  
  
</description><link>http://ayende.com/4802/concurrentdictionary-getoradd-may-call-the-valuefactory-method-more-than-once#comment1</link><guid>http://ayende.com/4802/concurrentdictionary-getoradd-may-call-the-valuefactory-method-more-than-once#comment1</guid><pubDate>Tue, 05 Apr 2011 09:35:08 GMT</pubDate></item></channel></rss>