Static methods and variables

G

Guest

Hi all,

I have just been reading up on threading and synchronisation which has
raised a question that I have not thought about before.

If you have a class with static members say in an ASP.NET application. Is
there any danger of thread race conditions as the members are not instance.

Same question with methods.

ie. ASP.NET session accesses a static method, another session accesses the
same static method. Is it possible that windows will time slice the accesses
therefore leaving the static member fields in an inconsistant state.

Same question with windows forms access to static members (providing another
thread hasn't purposely started an additional thread)

Thanks in advance.

Pete.
 
B

Bruno Jouhier [MVP]

trinitypete said:
Hi all,

I have just been reading up on threading and synchronisation which has
raised a question that I have not thought about before.

If you have a class with static members say in an ASP.NET application. Is
there any danger of thread race conditions as the members are not
instance.

Yes, of course.

Even if the members are instance, you are not always on the safe side. If
your instance is only accessed by the thread that created it, you are safe,
but if it can be shared by several threads (for ex if it is accessible
directly or indirectly from a static variable), you need to worry about
synchronization.

One exception though for static variables: if the variable is marked with
the ThreadStatic attribute, then each thread has its own copy of the
variable, and you are (usually) on the safe side.
Same question with methods.

Not necessarily. It all depends on what the static method is manipulating.
If it only manipulates objects that you pass through parameters and if these
objects are only accessed by your thread, you are on the safe side. But if
it manipulates static variables, you need to worry about synchronization.
ie. ASP.NET session accesses a static method, another session accesses the
same static method. Is it possible that windows will time slice the
accesses
therefore leaving the static member fields in an inconsistant state.
Yes!


Same question with windows forms access to static members (providing
another
thread hasn't purposely started an additional thread)

If your WinForms application is multi-threaded, yes! Multi-threading in
WinForms raises other issues, for example the fact that you have to use
Control.Invoke if you want to call a method on a control that was created by
another thread.
Thanks in advance.

Welcome to the wonderful (and dangerous) world of multi-threaded apps!

Bruno.
 
D

DT

Hi,

Sorry to barge in on this thread but I was going to post a very similar
question.

I'm guessing that this method isn't thread safe?

private static readonly HashTable ht = new HashTable();

public static HashTable GetItems(string key)
{
if (!ht.Contains(key))
{
ht.Add(key, value);
}
etc etc
}
 
B

Bruno Jouhier [MVP]

DT said:
Hi,

Sorry to barge in on this thread but I was going to post a very similar
question.

I'm guessing that this method isn't thread safe?

It is not. You should add a lock block:
private static readonly HashTable ht = new HashTable();

public static HashTable GetItems(string key)
{

lock (ht.SyncRoot) {
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top