Re: Microsoft Data Access Block and static methods

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Scott Allen (bitmask_at_[nospam)
Date: 12/10/04


Date: Fri, 10 Dec 2004 09:43:06 -0500

Hi Clint:

>
>So I don't have to worry about adding my own lock(...) commands when
>referencing MS-written static methods, but if I'm writing my own static
>methods that reference static fields in a class, I should use
>lock(...)?
>ie,
>

It's always good to check the docs, but generally the MS static
methods are thread safe.

>class Foo
>{
>private static string testValue;
>
>public static void AdjustTestValue()
>{
>lock (this) // or lock(typeof(Foo))?
>{
>testValue = "something";
>}
>}
>}

Yes, a lock would be needed. Remember you can't use "this" in a static
method. Jon has a good article on picking what to lock on:
http://www.yoda.arachsys.com/csharp/multithreading.html#lock.choice

>
>----------
>That being said, instance members are not safe because a single
>instance of an object is typically not going to be used by multiple
>threads. In ASP.NET for instance, you typically create an instance of a
>class for each user request - a TextBox control will never see multiple
>threads. Since locks add some performance overhead (and a lot of
>development and testing overhead), the decision makes a great deal of
>sense, even if it looks counterintuitive at first.
>----------
>
>I'm having trouble understanding this ... do you mean that in an
>ASP.NET (and, for my purpose, Web Services), I should be instantiating
>classes instead of using statics, or that it's ok to use statics in
>that each request is processed on it's own thread, so long as I don't
>actually STORE a value in a static field(ex, inside the static method,
>I perform a SQL query only, not storing the result of that query to a
>static field)?
>

It is OK to use static methods in a multithreaded app, the place to be
very careful is when using static fields.

--
Scott
http://www.OdeToCode.com/blogs/scott/


Relevant Pages

  • Re: Microsoft Data Access Block and static methods
    ... a lock would be needed. ... >classes instead of using statics, or that it's ok to use statics in ... It is OK to use static methods in a multithreaded app, ... very careful is when using static fields. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Static versus Singleton versus seperate Instantiations...
    ... Use public static methods instead of a ... > while working in a probject, you realise that some string handling stuff ... > - use a singleton structure, so that only one instance is ever created ... > I've been using public statics since the move to c# and wondered if this ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Microsoft Data Access Block and static methods
    ... environment and couldn't let static methods not be thread safe. ... Since locks add some performance overhead (and a lot of ... classes instead of using statics, or that it's ok to use statics in ... I perform a SQL query only, not storing the result of that query to a ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: private static
    ... For some reason I thought this wouldn't compile because the statics weren't ... >> not require an instantiation of the host class, but being private, ... > You can call private static methods from other static methods ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: C++ Function Static Initialization, Thread-Safe?
    ... and threading libraries (both in development and ... actually support thread-safe function static initialization? ... consequences of using such a lock. ... constructs statics), and deadlock. ...
    (comp.programming.threads)