Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Code Quiz: How well do you understand ThreadStatic?

I just ran into the most interesting bug in recent recollection.  What's wrong with the following code?

... public override IEnumerable<PropertyInfo> SignatureProperties { get { IEnumerable<PropertyInfo> properties; if (signaturePropertiesDictionary.TryGetValue(GetType(), out properties)) return properties; ... } } [ThreadStatic] private static readonly Dictionary<Type, IEnumerable<PropertyInfo>> signaturePropertiesDictionary = new Dictionary<Type, IEnumerable<PropertyInfo>>(); ...

And you're not allowed answering if you get S#arp Architecture forum emails...and no peaking if you're not!! ;)

Billy McCafferty


Posted 01-09-2009 4:51 PM by Billy McCafferty
Filed under: ,

[Advertisement]

Comments

Mitch Constantinescu wrote re: Code Quiz: How well do you understand ThreadStatic?
on 01-10-2009 1:54 AM

U can't initialize threadstatics

Billy McCafferty wrote re: Code Quiz: How well do you understand ThreadStatic?
on 01-10-2009 8:35 AM

Technically, the initialization that you see above works just fine.  I compiled the code and ran all my unit tests and everything worked just fine.

C-J Berg wrote re: Code Quiz: How well do you understand ThreadStatic?
on 01-10-2009 9:55 AM

I'm not sure what this code in particular is used for, but let me guess. It breaks if the operation isn't bound to a single thread, and perhaps if the thread is reused in a thread pool. ASP.NET requests can begin on one thread, block, and then resume on another. Many years ago I had to fix one of those bugs in a production web site. Not fun, even though I didn't introduce it myself...

Jay wrote re: Code Quiz: How well do you understand ThreadStatic?
on 01-10-2009 10:47 AM

I think the second thread to hit it would find the value to be null.

On a separate note: If this is for S#arp Architecture, is ThreadStatic something you really want to be using? Starting with the 2.0 framework, web requests have thread mobility, under heavy load you'll often see this.

What I typically do is create a wrapper class that delegates uses HttpContext.Current.Items if the call happens under a web request, otherwise it delegates to a ThreadStatic field.

sergiopereira wrote re: Code Quiz: How well do you understand ThreadStatic?
on 01-10-2009 12:06 PM

I think C-J Berg is right. It's never fun to use ThreadStatic in an ASP.NET context because you are not in control of the threads or thread pool. Under heavy load, your thread-static variable will exhibit promiscuous behavior ;)

Reshef wrote re: Code Quiz: How well do you understand ThreadStatic?
on 01-11-2009 2:58 AM

I think Jay is right. The dictionary will be initialized only for the first thread.

Billy McCafferty wrote re: Code Quiz: How well do you understand ThreadStatic?
on 01-11-2009 10:26 AM

Yup, C-J Berg implied it and Jay articulated it:  the second thread to hit it would find the value to be null.  I only suspected this was happening until I found the following post at blogs.msdn.com/.../670497.aspx which has a good description of the situation and ThreadStatic's usefulness.

WRT its use in S#arp Architecture, it's being used in a library which isn't used in just web contexts.  But if you'd be interested in joining in on the discussion, I've certainly learned alot about thread safe operations and the usefulness of ThreadStatic at groups.google.com/.../e01b179d3962d2e2

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)