Concurrency and static function and web application

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
 
A

Alvin Bruney

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
 

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

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top