When is it unsafe to use static members in your class?

  • Thread starter Thread starter N Erlich
  • Start date Start date
N

N Erlich

Hi all,

I understand the concept of static fields and members
inside a class and the fact that static members belong to
the class rather than to an instance of the class.

I also understand that each class that calls a static
member of another class takes a copy of that member into
its own thread and therefor it's safe to call a static
member concurrency wise.

I have 3 questions though:

1. Are my above understandings correct?

2. when is it unsafe to call/use static members in a class

3. when should one use static members and when shouldn't
he?

Any info would be greatly appreciated!
 
N Erlich said:
I understand the concept of static fields and members
inside a class and the fact that static members belong to
the class rather than to an instance of the class.
Yes.

I also understand that each class that calls a static
member of another class takes a copy of that member into
its own thread and therefor it's safe to call a static
member concurrency wise.

No. Where did you get that idea from? The fact that pretty much all the
..NET framework classes are marked in MSDN as having thread-safe static
methods doesn't mean that static methods are *inherently* thread-safe.
 
Thanks for your post,

It is my understatnding that an object instance calling a
static method in a class "takes" a copy of that method
into the calling thread.

This is for example how the microsoft's sql helper class
works, it's defined from static methods only although
multiple instances of objects can call these methods
simultaneously.

Nevertheless, I still not quite sure of when to use what
when it comes to static/non static members, can anyone
help with this one please?
 
Nir Erlich said:
It is my understatnding that an object instance calling a
static method in a class "takes" a copy of that method
into the calling thread.

What *exactly* do you mean by that? It sounds unlikely to me, but
without knowing exactly what you mean, it's hard to say.
 
What *exactly* do you mean by that? It sounds unlikely to me, but
without knowing exactly what you mean, it's hard to say.

The problem is that both threads may attempt to alter a variable at
relatively the same time. However, there is only one copy of a variable
on the stack/heap.

Consider:

private static int iCounter=0;
public static void Increment()
{
return iCounter++;
}

You may think that once a thread begins the first line of code it would
finish before another thread began to run it. But this is not
necassarily the case. The above statement becomes more than one
statement when compiled to MSIL code. The second thread may read the
previous value before the first thread actually copies the incremented
value back on the original variable location.

If two threads run you could may well get the intended values returned:
12

but you may get:
11

What you need to do is prevent the second thread from entering the first
line until the console has written the first threads value. There is
more than one way to accomplish this given the scenario, but we can't
explain all the possibilities here. I recommend you read every help file
there is on threading before you try to create a multi-threaded
application.

See the following for a start on threading:

"Threading Design Guidelines"
http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/cpgenref/html/cpconthreadingdesignguidelines.asp

"Threads and Threading"
http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/cpguide/html/cpconthreadsthreading.asp

When to use static methods vs instance methods is entirely different
issue from threading.

Michael Lang, MCSD
 
Back
Top