Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a value to a global variable at the same time from multiple threads?

How can I add value to a variable at the same time? If I can what result will be? crash or something else?
For example:

int a;

so there will be 2 thread to adding value once.

like image 430
Mustafa Ekici Avatar asked Nov 24 '25 09:11

Mustafa Ekici


2 Answers

If you do this in an uncontrolled fashion, it's quite possible that you'll lose data.

Each thread will take three steps:

  • Read the value
  • Increment the copy
  • Store the incremented value

If they both perform the first step roughly together, the result will be an increment of 1 instead of 2.

Additionally, there are memory model issues where one thread may not "see" the write from another thread. Memory models are complex beasts...

Use Interlocked.Increment to perform an atomic increment, which also uses volatile memory access to make sure it always sees what other threads have written.

Sample of broken code:

using System;
using System.Threading;

class Test
{
    const int Iterations = 1000000;

    static int counter;


    static void Main()
    {
        Thread t1 = new Thread(AddLots);
        t1.Start();
        AddLots();
        t1.Join();
        Console.WriteLine(counter);
    }

    static void AddLots()
    {
        for (int i = 0; i < Iterations; i++)
        {
            // Broken!
            counter++;
        }
    }
}

Running on my laptop just now, that showed a result of 1011788.

Change this line:

counter++;

to this:

Interlocked.Increment(ref counter);

and it all works beautifully.

like image 98
Jon Skeet Avatar answered Nov 26 '25 22:11

Jon Skeet


The behavior when 2 threads add to a shared value without any sort of synchronization is undefined. The write wont't cause any sort of crash. It will just leave the value as the final value as seen by one of the threads

In order to get defined behavior you need to add some sort of synchronization such as a lock.

internal static class Holder {
  static object m_lock = new object();
  static int a;
  internal static void Add(int value) {
    lock (m_lock) {
      a += value;
    }
  }
}
like image 30
JaredPar Avatar answered Nov 27 '25 00:11

JaredPar



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!