Is the code under App_code reentrant???

  • Thread starter Thread starter lander
  • Start date Start date
L

lander

That is, if multiple users are requesting or posting to the same page,
is the the code under app_code reentrant??? Why?

Thanks~!
 
lander said:
That is, if multiple users are requesting or posting to the same page,
is the the code under app_code reentrant??? Why?

I'm not sure you are using the term re-entrant appropriately I suspect you
mean is it threadsafe. The answer is: its up to you.

Normally classes that you may place in app_code that are then instanced by
ASPX pages will be threadsafe in that they are only being used by one thread
for the duration of the current request processing. However if you then
place instances such classes somewhere that can be accessed by more than one
thread (cache, static member etc) the responsibility for creating thread
safety is yours.
 
I'm not sure you are using the term re-entrant appropriately I suspect you
mean is it threadsafe. The answer is: its up to you.

Normally classes that you may place in app_code that are then instanced by
ASPX pages will be threadsafe in that they are only being used by one thread
for the duration of the current request processing. However if you then
place instances such classes somewhere that can be accessed by more than one
thread (cache, static member etc) the responsibility for creating thread
safety is yours.

Ok, i create one variable inside the page class, which is static and
its value is set in page_load, is it safe to do so?
 
lander said:
Ok, i create one variable inside the page class, which is static and
its value is set in page_load, is it safe to do so?

No.

Static variables will be available across the whole app domain. It is
conceivable that two clients hitting the same page at the same time would
have two threads trying to modify that variable at the same time.

You will need to add some synchronising code such as:-

static SomeType ourValue = null;
static readonly object ourLock = new object();

public static void ValueMutator()
{
lock(ourLock);
{
// code that modified ourValue;
}
}

Of course the exact nature of the locking needed (if any) will depend on the
page logic.
 
No.

Static variables will be available across the whole app domain. It is
conceivable that two clients hitting the same page at the same time would
have two threads trying to modify that variable at the same time.

You will need to add some synchronising code such as:-

static SomeType ourValue = null;
static readonly object ourLock = new object();

public static void ValueMutator()
{
lock(ourLock);
{
// code that modified ourValue;
}

}

Of course the exact nature of the locking needed (if any) will depend on the
page logic.

Thanks very much!
 
Back
Top