Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I was using such code:

class Counter
{
    private int i = 0;

    public int Next()
    {
        lock (this)
        {
            return i++;
        }
    }

    public void Reset()
    {
        lock (this)
        {
            i = 0;
        }
    }

}

I refactored it such a way:

class Counter
{
    private int i = 0;

    public int Next()
    {
        return Interlocked.Increment(ref i);
    }

    public void Reset()
    {
        i = 0;
    }

}

Will that work? I'm using .NET 4.5 if this matters.

share|improve this question
I would use Thread.VolatileWrite() (or Volatile.Write()) in your Reset(), but I'm not sure whether it's actually necessary. – svick Aug 28 '12 at 8:52
2  
Why would you make code harder to understand and possibly risk some kind of subtle issue for code that, at best, will function identically to the original code? – David Schwartz Aug 28 '12 at 13:49

2 Answers

up vote 0 down vote accepted

It should also be fine in earlier versions of .NET as you are not using any 4.5 specific classes or language features.

The reset is fine as the assignment is atomic as you mentioned, however if you were using a long then you would want to use Interlocked.Exchange because the assignment of a 64bit number is not atomic on a 32bit operating system (as it is stored as 2 32bit values).

The only changes I would suggest are purely from a coding standards perspective:

// seal the class as it's not designed to be inherited from.
public sealed class Counter
{
    // use a meaningful name, 'i' by convention should only be used in a for loop.
    private int current = 0;

    // update the method name to imply that it returns something.
    public int NextValue()
    {
        // prefix fields with 'this'
        return Interlocked.Increment(ref this.current);
    }

    public void Reset()
    {
        this.current = 0;
    }
}
share|improve this answer
are you saying that for next value you can use increment, but for resetting not used any lock stuffs. I don't it will work. Please explain better. sorry for my ignorance. – sarat Oct 27 '12 at 11:11
You have to use Interlocked for NextValue because you need to read and write the current value as a single atomic operation, for reset you are only doing a write which is an atomic operation on its own. – Trevor Pilley Oct 27 '12 at 11:43

In the reset method, use System.Threading.Interlocked.Exchange(ref i, 0);

share|improve this answer
2  
assign is atomic operation, isn't it? why do you want to use that? – javapowered Aug 31 '12 at 20:22
1  
MaLio, welcome to Code Review! Please try to add more explanations to your answer(s). :) – Quentin Pradet Sep 21 '12 at 14:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.