4

I'm looking for some advice on writing some thread safe, optimized, elegant code to do the following:

I want a static method to return a sequence of integers. So for example, the application starts, thread 1 calls the GetSequence method and says it wants to take 3, so it gets an integer array back consisting of 0,1,2. Thread 2 then calls the method and says give me 4, so it returns 3,4,5,6. Multiple threads can simultaneously call this method.

To give an idea of the sort of thing I'm thinking of, here's my attempt at this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SequenceNumberService
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] numbers = NumberSequenceService.GetSequence(3);

            foreach (var item in numbers)
            {
                Console.WriteLine(item.ToString());
            }

            // Writes out:
            // 0
            // 1
            // 2

            Console.ReadLine();
        }
    }

    public static class NumberSequenceService
    {
        private static int m_LastNumber;
        private static object m_Lock = new Object();

        public static int[] GetSequence(int take)
        {
            int[] returnVal = new int[take];
            int lastNumber;

            // Increment the last audit number, based on the take value.
            // It is here where I am concerned that there is a threading issue, as multiple threads
            // may hit these lines of code at the same time.  Should I just put a lock around these two lines
            // of code, or is there a nicer way to achieve this.
            lock (m_Lock)
            {
                m_LastNumber = m_LastNumber + take;
                lastNumber = m_LastNumber;
            }

            for (int i = take; i > 0; i--)
            {
                returnVal[take - i] = lastNumber - i;
            }

            return returnVal;
        }
    }
}

My questions therefore are: Am I approaching this in the best way, or is there another way to achieve this? Any suggestions for optimizing this code?

Many thanks in advance for any help.

1
  • I think your approach looks pretty good.
    – dana
    Commented Feb 16, 2012 at 21:42

1 Answer 1

8

You probably want to look into the Interlocked class and it's Increment and Add methods:

public static Int32 num = 0;

public static Int32 GetSequence() 
{ 
    return Interlocked.Increment(ref num); 
}

public static IEnumerable<Int32> GetSequenceRange(Int32 count) 
{ 
    var newValue = Interlocked.Add(ref num, count);
    return Enumerable.Range(newValue - count, count);
}
3
  • 2
    If two threads ask for 3 numbers each at the same time, this might return {0,2,4} and {1,3,5}. I think the question asks for each sequence to be contiguous. Commented Feb 16, 2012 at 21:40
  • Something where the thread works out the next number, and then uses Interlocked.CompareExchange to "reserve" that range should do the trick... Commented Feb 16, 2012 at 21:42
  • Thanks Chris, that's exactly what I was after.
    – P2l
    Commented Feb 16, 2012 at 22:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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