weird behavior with static int

P

parez

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();
 
G

Gilles Kohl [MVP]

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

parez

You'll need to post a concise-but-complete code sample that demonstrates
the problem.


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
 
B

Brian Gideon

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.
 

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

Top