Concurrency and static function and web application

Discussion in 'ASP .Net' started by Homa, Nov 21, 2003.

  1. Homa

    Homa Guest

    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
    Homa, Nov 21, 2003
    #1
    1. Advertising

  2. Homa

    Alvin Bruney Guest

    > 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
    --


    -----------
    Got TidBits?
    Get it here: www.networkip.net/tidbits
    "Homa" <> wrote in message
    news:...
    > 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
    Alvin Bruney, Nov 21, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sami Vaaraniemi
    Replies:
    0
    Views:
    528
    Sami Vaaraniemi
    Apr 1, 2004
  2. Billy Jiggs
    Replies:
    0
    Views:
    448
    Billy Jiggs
    Apr 1, 2004
  3. Uli
    Replies:
    1
    Views:
    2,949
    Gerbrand van Dieijen
    Jul 4, 2003
  4. Replies:
    2
    Views:
    504
    Jonathan Mcdougall
    Aug 6, 2007
  5. Jordan S.
    Replies:
    6
    Views:
    358
    Jordan S.
    Jun 2, 2008
Loading...

Share This Page