Static methods

  • Thread starter Thread starter Simon Harvey
  • Start date Start date
S

Simon Harvey

Hi all,

I'm considering using a the micorsoft data access block as the main DAC for
my application. A colleage of mine suggested that I might have a problem
with concurrency though

When a method is static, do I need to worry about concurrency?

I figured that microsoft would have had this in mind when making the
component. Obviously numerous users are going to be routing calls through
this component at the same time.

Should I be worried about this and what can I do if it is going to be a
problem? The system will sustain about 200 users at any one time

Thanks for your help

Simon
 
Simon Harvey said:
I'm considering using a the micorsoft data access block as the main DAC for
my application. A colleage of mine suggested that I might have a problem
with concurrency though

When a method is static, do I need to worry about concurrency?

Absolutely. There could be more than one thread accessing the static
method at a time. That doesn't mean you can't make it thread-safe, but
it's not going to be automatically thread-safe just by being static.
 
Hi Jon,

What would the simplest and most effective mechanism for making the method
thread safe?

Thanks for your help

Simon
 
Hi all,

I'm considering using a the micorsoft data access block as the main
DAC for my application. A colleage of mine suggested that I might have
a problem with concurrency though

When a method is static, do I need to worry about concurrency?

Any method that does not handle any data external to the method is by
default thread safe, or at least as thread-safe as the parameters to it
are.

As such, a static method that only operates on the parameters it's given,
and those parameters are also thread-safe in the sense that no other
thread is operating on them at the same time, then that method will be
thread safe. Additionally, any local variables held in the class must
also be thread safe in the sense that they must not be depending on other
instances of objects than themselves. For instance, if you create a new
object inside the method, that's ok. If that object adds itself to a
static/shared list somewhere, that code must be thread-safe or you will
have a problem.

If the method (static or not) operates on anything outside of this, like
member variables (static or not), external system resources (files,
serial ports, etc.), then that method must be written specifically with
thread safety in mind.
 
Back
Top