471,328 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,328 software developers and data experts.

weird behavior with static int

Hi,
GetNextSequenceNumber2 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 GetNextSequenceNumber1()
{

lock (padlock)
{
return ++_sequenceNumber;
}
}
private static int GetNextSequenceNumber2()
{

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

private static int _sequenceNumber = 0;

static readonly object padlock = new object();
Jun 27 '08 #1
4 1121
On Thu, 24 Apr 2008 15:49:35 -0700, parez <ps*****@gmail.comwrote:
GetNextSequenceNumber2 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
Jun 27 '08 #2
On Thu, 24 Apr 2008 15:49:35 -0700 (PDT), parez <ps*****@gmail.comwrote:
>GetNextSequenceNumber2 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 GetNextSequenceNumber1()
{

lock (padlock)
{
return ++_sequenceNumber;
}
}
private static int GetNextSequenceNumber2()
{

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 = GetNextSequenceNumber1();

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

If we do this (sequenceNumber again being 42):

int value = GetNextSequenceNumber2();

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.

Jun 27 '08 #3
On Apr 24, 7:21 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Thu, 24 Apr 2008 15:49:35 -0700, parez <psaw...@gmail.comwrote:
GetNextSequenceNumber2 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 GetNextSequenceNumber()
{

lock (padlock)
{
return _sequenceNumber++;
}
}
I was doing something stupid..

I had a
_sequenceNumber=ClassName.GetNextSequenceNumber();

thats _sequenceNumber++ didnt work and ++_sequenceNumber; 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
Jun 27 '08 #4
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.
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by DaKoadMunky | last post: by
11 posts views Thread by Les Paul | last post: by
11 posts views Thread by ncf | last post: by
3 posts views Thread by Holger (David) Wagner | last post: by
13 posts views Thread by Kenneth Baltrinic | last post: by
1 post views Thread by Jonathan Yong | last post: by
4 posts views Thread by Miro | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.