Multi-threading and static classes

  • Thread starter Thread starter William Stacey [MVP]
  • Start date Start date
W

William Stacey [MVP]

Anytime you share a resource (such as an array) that multiple threads could
update, you need to sync using monitor or something else - it does not
matter if method is static or not. I am not sure if ASP serializes method
calls or not. If not, then you need to use a lock.
 
I have a static class that I'll be using in an ASP.NET app. One of the
static methods returns an array based on a user ID. Pseudo-code is as follows:

static string[] myFunc(string userID)
{
declare array
populate array with value from a database; filter by userID param.
return the array to the function caller
}

My question is this: Is this method susceptible to a race condition? Do I
need to lock the array within the static function so that another thread
won't mess up the array values? Please remember that this class will be used
w/in an ASP.NET app. Thanks =)
 
jester said:
I have a static class that I'll be using in an ASP.NET app. One of the
static methods returns an array based on a user ID. Pseudo-code is as follows:

static string[] myFunc(string userID)
{
declare array
populate array with value from a database; filter by userID param.
return the array to the function caller
}

My question is this: Is this method susceptible to a race condition? Do I
need to lock the array within the static function so that another thread
won't mess up the array values? Please remember that this class will be used
w/in an ASP.NET app. Thanks =)

Interesting question. I think it's safe. As 'array' isn't static, each
ASP.NET thread
will get it's own copy.

If 'array' is static by virtue of the fact that it's in a static block (I
don't think that's the case),
then indeed you would need to use ThreadLocal or a lock.

The same applies if myFunc ever starts to touch any other member vars.

Ben
 
Hmmm. Your pseudo code is not 100% clear.

But I guess what you mean is this:

static string[] myFunc( string userID )
{
string[] retVal = new string[ sizeOfArray ];
...
return retVal;
}

In that case you have nothing to worry about.
Every time you call the function, a new array is created on the heap. A
reference to the array is stored on the stack (that is you retVal variable.
Is is a variable on the stack, that references where the data is in the
heap).
Since each thread has its own stack, there is no problem.

If two different threads are calling this function simultaneously, they will
get two different arrays returned.

Pete
 
Thanks to all of you guys. I was also thinking along the lines that since
the array is local, then each calling thread will not interfere with others.
Not really sure before but I feel better now. Thanks again =)

Peter Strøiman said:
Hmmm. Your pseudo code is not 100% clear.

But I guess what you mean is this:

static string[] myFunc( string userID )
{
string[] retVal = new string[ sizeOfArray ];
...
return retVal;
}

In that case you have nothing to worry about.
Every time you call the function, a new array is created on the heap. A
reference to the array is stored on the stack (that is you retVal variable.
Is is a variable on the stack, that references where the data is in the
heap).
Since each thread has its own stack, there is no problem.

If two different threads are calling this function simultaneously, they will
get two different arrays returned.

Pete

jester said:
I have a static class that I'll be using in an ASP.NET app. One of the
static methods returns an array based on a user ID. Pseudo-code is as
follows:

static string[] myFunc(string userID)
{
declare array
populate array with value from a database; filter by userID param.
return the array to the function caller
}

My question is this: Is this method susceptible to a race condition? Do I
need to lock the array within the static function so that another thread
won't mess up the array values? Please remember that this class will be
used
w/in an ASP.NET app. Thanks =)
 
Back
Top