I've had a bit of a locking/threading theme in some of my recent postings so I thought I'd continue that with a little problem.  Here's a (simplified) set of operations that is sort of an example of what you might need to do in a free threaded class.  Let's assume there is one instance of this class that is to be shared between multiple threads any of which might call the DoIt() function at any time.

Here are two possible implementations that are like things I've seen in the past.  The question is:  Which of these is "better?"  Is there something that is (in some sense) better still?  What are the factors that influence this decision (Hint: see the title of this posting)

You might have to scroll down to see the code depending on your browser because the <PRE> formatting seems to come out weird with the stylesheets in use here.

    // it's easy to explain what this does
    class ExampleCoarse
    {
        public ExampleCoarse() {}
        private Object myLock = new Object();
        private int count;

        public void DoIt(StreamWriter sw)
        {
            lock(myLock)
            {
                long ticks = DateTime.Now.Ticks;
                int count = IncrementCount();
                LogToStream(sw, ticks, count);
            }
        }

        private int IncrementCount()
        {
            return ++count;
        }

        private void LogToSteam(StreamWriter sw, long ticks, int count)
        {
            sw.WriteLine("{0} {1}", ticks, count);
        }
    }

    // it's not so easy to explain this... or how to use it.. but is it better?
    class ExampleFine
    {
        public ExampleFine() {}
        private Object myLock = new Object();
        private int count;

        public void DoIt(StreamWriter sw)
        {
            long ticks = DateTime.Now.Ticks;
            int count = IncrementCount();
            LogToStream(sw, ticks, count);
        }

        private int IncrementCount()
        {
            int c = System.Threading.Interlocked.Increment(count);
            return c;
        }

        private void LogToSteam(StreamWriter sw, long ticks, int count)
        {
            lock (myLock)
            {
                sw.WriteLine("{0} {1}", ticks, count);
            }
        }
    }