3

Data has to be shared between threads. Which is the best synchronization method?

Does the lock is the better approach or Mutex?

namespace ConsoleApplication8
{
    class Data
    {
        public int X
        {
            set;
            get;
        }

        public int Y
        {
            get;
            set;
        }

        public int result;
    }

    class Program
    {
        static int a;
        private static object aLock = new object();

        static void Main(string[] args)
        {
            ParameterizedThreadStart aStart = new ParameterizedThreadStart(Addition);
            Thread aThread = new Thread(aStart);
            Data aData = new Data();
            aData.X = 10;
            aData.Y = 20;

            Thread aThread2 = new Thread(aStart);
            aThread2.Start();


            aThread.Start(aData);
            aThread.Join();
            aThread2.Join();
            Console.WriteLine("End of the program");
        }

        static void Addition(object data)
        {
            var a = data as Data;
            var b = a.X + a.Y;
            a.result = b;

            Console.WriteLine(a.result);
            Thread.Sleep(1000);
            Console.WriteLine("End of thread");
            updateValue();
        }

        static void updateValue()
        {
            lock (aLock)
            {
                a++;
            }
        }
    }
}
Suman Banerjee
  • 1,923
  • 4
  • 24
  • 40
Raghaav
  • 243
  • 1
  • 2
  • 5
  • 4
    Define "better" criterias. Is apple better than orange? – zerkms Sep 08 '11 at 05:18
  • 1
    Or do you just want to know the difference of lock and mutex? – erikH Sep 08 '11 at 05:23
  • look at same these post : http://stackoverflow.com/questions/1164038/monitor-vs-mutex-in-c http://stackoverflow.com/questions/1763251/lock-monitor-mutex – Rev Sep 08 '11 at 05:24
  • @erikH: what advantages I will get if I use Lock or Mutex when sync the shared data? – Raghaav Sep 08 '11 at 05:30
  • Mutex can be used for interprocess syncronization. The lock statement is more ligthweigth and rely on a Monitor. – erikH Sep 08 '11 at 05:39

1 Answers1

5

You have two places where you are "synchronizing" threads.

You're using Thread.Join to wait for your threads to complete before continuing your main thread. That's fine.

You're also using a lock to make sure that only one thread at a time increments your counter variable a. That's also fine, but can be improved. There's a class called Interlocked in System.Threading that can perform the increment for you in a thread-safe manner.

Interlocked.Increment(ref a);

Your code does use the variable a in two places - inside Addition you have a local variable a that shadows the static outer variable a. I assume this is just a coincidence.

Another issue is that your Addition method takes an object as a parameter. I understand why it's not taking Data as the parameter because ParameterizedThreadStart requires an object. There is a better way around this.

Try this code instead:

private static int __counter = 0;

public class Data
{
    public int X { set; get; }
    public int Y { set; get; }
    public int Result { set; get; }
}

private void Addition(Data data)
{
    data.Result = data.X + data.Y;
    Interlocked.Increment(ref __counter);

    Thread.Sleep(1000);

    Console.WriteLine(data.Result);
    Console.WriteLine("End of thread");
}

Now Main can be written as:

void Main()
{
    ParameterizedThreadStart pts = o => Addition(o as Data);

    var t1 = new Thread(pts);
    var t2 = new Thread(pts);

    t1.Start(new Data { X = 10, Y = 20 });
    t2.Start(new Data { X = 13, Y = 42 });

    t1.Join();
    t2.Join();

    Console.WriteLine(__counter);
    Console.WriteLine("End of the program");
}

That should be a little neater.

I don't see exactly what data you are sharing in your code. Maybe you could elaborate?

Enigmativity
  • 113,464
  • 11
  • 89
  • 172