Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2007

Tip: Looking for answers? Try searching our database.

testing static member - thread safe?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
bbg - 26 Sep 2007 09:06 GMT
If many thread needs to test if one field of static class is null,

  if (StaticClass.fieldA == null)

will it be thread safe and doesn't need thread synchronization?

Bob
Jon Skeet [C# MVP] - 26 Sep 2007 09:14 GMT
> If many thread needs to test if one field of static class is null,
>
>    if (StaticClass.fieldA == null)
>
> will it be thread safe and doesn't need thread synchronization?

Only if fieldA is either readonly (and initialized as part of the
class initialization for StaticClass) or volatile.

Otherwise you could end up seeing "stale" values.

Jon
Michael Starberg - 27 Sep 2007 23:59 GMT
>> If many thread needs to test if one field of static class is null,
>>
[quoted text clipped - 8 lines]
>
> Jon

bbg:
.. and you have to think about if old/stale values are a problem.

They usually are.

This goes further than just threading.
These are my idiom:

Always treat a property once.

int someCounter = myList.Count;
SomeClass.MyStaticMethod(someCounter);
for (int i; i < myList.Count; i++)  // <- BAD, You called the property
twice.
{
  // a property can do anything, even being recursive.
  // it may or may not take time to complete.
  // it may use DateTime or a GUID
  // so try to take it into local scope once, and use it close.

 // as what is not shown here, is that myList could be
 // used by other threads, with maybe weird bugs as a result.
}

If you know you are using threads, you should lock before use.
I'd default to thinking nothing is thread-safe
unless the documentation promises me othervise.

But back to where I started.
Let's say I have a visit-counter on a webpage.
Let's say I 'cache' this number in StaticClass.fieldA.

Does it really matter if one user gets the correct 1234 users visited,
and other user gets 1233 stale count of users visited?
Will they call eachother up and ask about the difference?

I've done code where I knew I'd get stale answers,
but instead of using volatile, or locking, I was like Bond || Cartney;
- Live and let die.. or... live!

// Michael Starberg

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.