weird behavior with static int

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • parez

    weird behavior with static int

    Hi,


    GetNextSequence Number2 worked for me in a different class..
    but now it does not work.. it does not increment..


    will it not get incremented after the return?


    private static int GetNextSequence Number1()
    {

    lock (padlock)
    {
    return ++_sequenceNumb er;
    }
    }


    private static int GetNextSequence Number2()
    {

    lock (padlock)
    {
    return _sequenceNumber ++;
    }
    }

    private static int _sequenceNumber = 0;

    static readonly object padlock = new object();
  • Peter Duniho

    #2
    Re: weird behavior with static int

    On Thu, 24 Apr 2008 15:49:35 -0700, parez <psawant@gmail. comwrote:
    GetNextSequence Number2 worked for me in a different class..
    but now it does not work.. it does not increment..
    You'll need to post a concise-but-complete code sample that demonstrates
    the problem.
    will it not get incremented after the return?
    In either version of the method you posted, the variable will be
    incremented _before_ the return. In one version, it's incremented before
    being evaluated as the return value, and in the other it's incremented
    after. But in either case, the variable will have its new value before
    control is returned to the caller.

    In neither version is there an obvious problem, so whatever is going
    wrong, it's in a part of the code you didn't show us. Either the code you
    posted isn't actually the code that's generating the sequence numbers, or
    you're mistaken about whether it works or not.

    The only way for anyone to figure out what's actually going wrong is to
    see a complete code example. Making it concise will ensure that someone
    will bother looking at it. :)

    By the way, for what it's worth, if all you're doing is incrementing a
    variable, you may find the Interlocked class a better solution than using
    the lock() statement. Not that there's anything wrong with using lock()
    per se; just that you might prefer the alternative.

    Pete

    Comment

    • Gilles Kohl [MVP]

      #3
      Re: weird behavior with static int

      On Thu, 24 Apr 2008 15:49:35 -0700 (PDT), parez <psawant@gmail. comwrote:
      >GetNextSequenc eNumber2 worked for me in a different class..
      >but now it does not work.. it does not increment..
      >
      >
      >will it not get incremented after the return?
      >
      >
      private static int GetNextSequence Number1()
      {
      >
      lock (padlock)
      {
      return ++_sequenceNumb er;
      }
      }
      >
      >
      private static int GetNextSequence Number2()
      {
      >
      lock (padlock)
      {
      return _sequenceNumber ++;
      }
      }
      >
      private static int _sequenceNumber = 0;
      >
      static readonly object padlock = new object();
      If sequenceNumber is currently 42 and we do this:

      int value = GetNextSequence Number1();

      after the call, both value and _sequenceNumber will be 43.

      If we do this (sequenceNumber again being 42):

      int value = GetNextSequence Number2();

      after the call, value will still be 42, but _sequenceNumber will have been incremented.

      That's just the way postincrement works.

      Might that be the problem?

      Regards,
      Gilles.

      Comment

      • parez

        #4
        Re: weird behavior with static int

        On Apr 24, 7:21 pm, "Peter Duniho" <NpOeStPe...@nn owslpianmk.com>
        wrote:
        On Thu, 24 Apr 2008 15:49:35 -0700, parez <psaw...@gmail. comwrote:
        GetNextSequence Number2 worked for me in a different class..
        but now it does not work.. it does not increment..
        >
        You'll need to post a concise-but-complete code sample that demonstrates
        the problem.
        >
        will it not get incremented after the return?
        >
        In either version of the method you posted, the variable will be
        incremented _before_ the return. In one version, it's incremented before
        being evaluated as the return value, and in the other it's incremented
        after. But in either case, the variable will have its new value before
        control is returned to the caller.
        >
        In neither version is there an obvious problem, so whatever is going
        wrong, it's in a part of the code you didn't show us. Either the code you
        posted isn't actually the code that's generating the sequence numbers, or
        you're mistaken about whether it works or not.
        >
        The only way for anyone to figure out what's actually going wrong is to
        see a complete code example. Making it concise will ensure that someone
        will bother looking at it. :)
        >
        By the way, for what it's worth, if all you're doing is incrementing a
        variable, you may find the Interlocked class a better solution than using
        the lock() statement. Not that there's anything wrong with using lock()
        per se; just that you might prefer the alternative.
        >
        Pete
        private static int _sequenceNumber = 0;

        static readonly object padlock = new object();


        #endregion

        private static int GetNextSequence Number()
        {

        lock (padlock)
        {
        return _sequenceNumber ++;
        }
        }


        I was doing something stupid..

        I had a
        _sequenceNumber =ClassName.GetN extSequenceNumb er();

        thats _sequenceNumber ++ didnt work and ++_sequenceNumb er; did.

        I refactored some of my existing code, used copy-paste in the process
        and really screwed up the refactoring..

        I will look into interlocked classes..

        Thanks


        Comment

        • Brian Gideon

          #5
          Re: weird behavior with static int

          On Apr 25, 9:49 am, parez <psaw...@gmail. comwrote:
          I will look into interlocked classes..
          >
          Thanks
          One word of caution...if you have another method in the class that is
          reading _sequenceNumber more than once inside a lock(padLock)
          statement then it's reasonable to say that you were implicitly
          assuming the value would stay the same between reads. However, if you
          convert the increment statement to use the Interlocked class your
          assumption may fail possibly resulting in a bug that is difficult to
          find.

          Comment

          Working...