Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread synchronization

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++;
            }
        }
    }
}
like image 376
Raghaav Avatar asked Dec 05 '25 01:12

Raghaav


1 Answers

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?

like image 169
Enigmativity Avatar answered Dec 07 '25 15:12

Enigmativity



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!