<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.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>Another bad lock pattern</title><link>http://blogs.msdn.com/ricom/archive/2003/12/06/41779.aspx</link><description>A while ago I asked Dr. GUI to post an MSDN article about the perils of using lock(typeof(Foo)), you can still find that article here . But recently I've started seeing another pattern that's just as bad: class MyClass { private static String myLock =</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Another bad lock pattern</title><link>http://blogs.msdn.com/ricom/archive/2003/12/06/41779.aspx#41830</link><pubDate>Sun, 07 Dec 2003 15:25:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:41830</guid><dc:creator>Dejan Jelovic</dc:creator><description>Are you regretting your decission to enable any object to be used as a mutex yet? ;&amp;gt;</description></item><item><title>re: Another bad lock pattern</title><link>http://blogs.msdn.com/ricom/archive/2003/12/06/41779.aspx#41856</link><pubDate>Sun, 07 Dec 2003 20:33:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:41856</guid><dc:creator>Rico Mariani</dc:creator><description>no comment :)</description></item><item><title>re: Another bad lock pattern</title><link>http://blogs.msdn.com/ricom/archive/2003/12/06/41779.aspx#41968</link><pubDate>Mon, 08 Dec 2003 11:33:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:41968</guid><dc:creator>Ian Ringrose</dc:creator><description>When I implement a singleton in C#, I often use typeof(class), is this a real problem.  I do not see how it can give a dead lock.  After all one else should be using my class in a lock statement.  I agree with using a separate lock object if the locking is any more complex then this.&lt;br&gt;&lt;br&gt;class A&lt;br&gt;(&lt;br&gt;   private A()&lt;br&gt;   {&lt;br&gt;       // some complex code...&lt;br&gt;   }&lt;br&gt;   &lt;br&gt;   public static A Get()&lt;br&gt;   {&lt;br&gt;      if (msOneAnyOnly == null)&lt;br&gt;      {&lt;br&gt;         lock(typeof(A))&lt;br&gt;         {&lt;br&gt;            if (msOneAnyOnly == null)&lt;br&gt;            {&lt;br&gt;               msOneAnyOnly = new A();&lt;br&gt;            }&lt;br&gt;         }&lt;br&gt;      }&lt;br&gt;      &lt;br&gt;      return msOneAnyOnly;&lt;br&gt;   }&lt;br&gt;   &lt;br&gt;   private A msOneAnyOnly;&lt;br&gt;)</description></item><item><title>re: Another bad lock pattern</title><link>http://blogs.msdn.com/ricom/archive/2003/12/06/41779.aspx#41984</link><pubDate>Mon, 08 Dec 2003 12:51:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:41984</guid><dc:creator>Rico Mariani</dc:creator><description>There are three problems with lock(typeof(A)):&lt;br&gt;&lt;br&gt;First: type objects are agile, so the same type object is used in all appdomains.  That means if your code is running in two different appdomains they will share the same lock which is probably a bad idea.&lt;br&gt;&lt;br&gt;Second: type objects are public, so anyone anywhere can do lock(typeof(A)) which could totally mess your algorithm up.&lt;br&gt;&lt;br&gt;Third: typeof() isn't free, you'll have to drag in a bunch of reflection code.&lt;br&gt;&lt;br&gt;So, avoiding lock(typeof(X)) results in faster, simpler, and more correct code.  In contrast I can't think of any benefits to using lock(typeof(X)) under any circumstances.</description></item><item><title>re: Another bad lock pattern</title><link>http://blogs.msdn.com/ricom/archive/2003/12/06/41779.aspx#41985</link><pubDate>Mon, 08 Dec 2003 12:58:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:41985</guid><dc:creator>Rico Mariani</dc:creator><description>Clarification: In your particular example the first problem doesn't matter much.  Really the third is the big issue.  It's much cheaper to have a static variable that holds the lock explicitly.</description></item><item><title>re: Another bad lock pattern</title><link>http://blogs.msdn.com/ricom/archive/2003/12/06/41779.aspx#65558</link><pubDate>Sat, 31 Jan 2004 20:47:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:65558</guid><dc:creator>Wesner Moise</dc:creator><description>In an NGEN app, doesn't using a static initializer introduce new additional checks before every function call?&lt;br&gt;&lt;br&gt;Thus, in fixing the lock, we slowed the class down.</description></item><item><title>re: Another bad lock pattern</title><link>http://blogs.msdn.com/ricom/archive/2003/12/06/41779.aspx#65562</link><pubDate>Sat, 31 Jan 2004 21:15:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:65562</guid><dc:creator>Rico Mariani</dc:creator><description>I think typically not.  If you put the test in the constructor for instance, then you mostly don't have to worry about instance methods being called having not initialized the statics because if you have an instance then you necessarily have run the constructor.&lt;br&gt;&lt;br&gt;There are complicated rules for this but it ends up not costing too much.  Also this is only an issue for domain nuetral code.&lt;br&gt;&lt;br&gt;In any case, the cost of typeof() is greater than just a single test... even if there was an extra test in every method that used the lock, it would still be faster.</description></item><item><title>re: Another bad lock pattern</title><link>http://blogs.msdn.com/ricom/archive/2003/12/06/41779.aspx#65565</link><pubDate>Sat, 31 Jan 2004 21:21:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:65565</guid><dc:creator>Rico Mariani</dc:creator><description>Actually, I think going into the exact details of this process would make an interesting blog/article.  First I'll see if someone has already done a good job on this topic.</description></item><item><title>Integrated mode roadblock: Request is not available in this context exception in Application_Start</title><link>http://blogs.msdn.com/ricom/archive/2003/12/06/41779.aspx#6081672</link><pubDate>Sun, 11 Nov 2007 05:43:20 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6081672</guid><dc:creator>Mike Volodarsky's ServerSide</dc:creator><description>&lt;p&gt;The &amp;amp;ldquo;Request is not available in this context&amp;amp;rdquo; exception is one of the most common errors&lt;/p&gt;
</description></item><item><title>System.Web.HttpException: Request is not available in this context - Asp.Net Classifieds Starter Kit Does Not Run in IIS7</title><link>http://blogs.msdn.com/ricom/archive/2003/12/06/41779.aspx#8773385</link><pubDate>Sat, 26 Jul 2008 02:34:19 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8773385</guid><dc:creator>Blog By Regan </dc:creator><description>&lt;p&gt;As we have found in deploying 2 projects to the new release of IIS in the past month that there were&lt;/p&gt;
</description></item><item><title>IIS7 Integrated mode: Request is not available in this context exception in Application_Start</title><link>http://blogs.msdn.com/ricom/archive/2003/12/06/41779.aspx#8773746</link><pubDate>Sat, 26 Jul 2008 04:34:20 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8773746</guid><dc:creator>Blog By Regan </dc:creator><description>&lt;p&gt;As we have found in deploying 2 projects to the new release of IIS in the past month that there were&lt;/p&gt;
</description></item><item><title>IIS7 Integrated mode: Request is not available in this context exception in Application_Start</title><link>http://blogs.msdn.com/ricom/archive/2003/12/06/41779.aspx#8782699</link><pubDate>Mon, 28 Jul 2008 08:56:29 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8782699</guid><dc:creator>Regan Schroder</dc:creator><description>&lt;p&gt;The “Request is not available in this context” exception is one of the more common errors you may receive&lt;/p&gt;
</description></item><item><title>lock file in ASP.NET | keyongtech</title><link>http://blogs.msdn.com/ricom/archive/2003/12/06/41779.aspx#9363879</link><pubDate>Thu, 22 Jan 2009 09:49:35 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9363879</guid><dc:creator>lock file in ASP.NET | keyongtech</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://www.keyongtech.com/549706-lock-file-in-asp-net"&gt;http://www.keyongtech.com/549706-lock-file-in-asp-net&lt;/a&gt;&lt;/p&gt;
</description></item></channel></rss>