Concurrency and static function and web application

  • Thread starter Thread starter Homa
  • Start date Start date
H

Homa

Hi,
It just come to my mine about conncurreny and static functions. This
would not be a problem in window application because each thread has
it own stack (and heap? forgot...).

I'm using some static function in a web application. If the function
take a bit of time to complete and when two sessions call the same
function, what would happen?
For example:

class cfoo
{
static int i = 0;

public static void foo()
{
i++;
...
// do something with i
}
}

When two sessions call foo() at the same time, and both processed i++
but not // do something with i, then the value is not correct (unless
that's what intended)

I understand the above example, but how about the following?

class cfoo
{
public static void foo(int i)
{
// do something
.....
// do something with i
}
}

in this case, when two sessions call foo(int i) at the same time but
with different values, what would happen? Will the function work as
normal? Or it will behave unpredictable?


Thank for concern,
Homa Wong
 
It just come to my mine about conncurreny and static functions. This
would not be a problem in window application because each thread has
it own stack (and heap? forgot...).

absolutely false. they suffer the same malaise.

It will have unpredictable results. Classic race condition. When using
anytype of global variable, you must take care to synchronize access. But I
think you already knew that.

Regards
 
Back
Top