none
How to increment by 5 using Interlocked.Increment RRS feed

  • Question

  • i saw Interlocked.Increment. does it always increment by 1 ? what to change as a result Interlocked.Increment does the increment by 5 or 10 ?
    Wednesday, January 16, 2019 6:04 PM

Answers

  • Hi,

    yes it increments by 1. Maybe you want to use Add instead.

    public static int Increment(ref int location);
    
    public static int Add(ref int location1, int value);

    Example:

    i = Interlocked.Add(ref j, 5);

    Here is the documentation of the method:

    https://docs.microsoft.com/en-US/dotnet/api/system.threading.interlocked.increment?view=netframework-4.7.2

    • Edited by DerChris88 Wednesday, January 16, 2019 8:38 PM
    • Marked as answer by Sudip_inn Thursday, January 17, 2019 3:36 PM
    Wednesday, January 16, 2019 8:33 PM
  • Hi Sudip_inn,

    Thank you for posting here.

    For your question, the Interlocked.Increment Method do something like number++ in the code. This method is the same as Add, but they use the value of 1 as an implicit argument. It could not be used to increase by 5.

    As DerChris88 said, you could use Add method to instead to increase with 5. Here is a simple example for your reference.

    Run the A method twice, it will return 10.

    class Program
        {
            static int _value;
            static void Main(string[] args)
            {
                Thread thread1 = new Thread(new ThreadStart(A));
                Thread thread2 = new Thread(new ThreadStart(A));
                thread1.Start();
                thread2.Start();
                thread1.Join();
                thread2.Join();
                Console.WriteLine(_value);
                Console.ReadKey();
            }
    
            static void A()
            {
                // Add 5.
                Interlocked.Add(ref _value, 5);
            }
        }

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Thursday, January 17, 2019 3:36 PM
    Thursday, January 17, 2019 7:07 AM
    Moderator

All replies

  • Hi,

    yes it increments by 1. Maybe you want to use Add instead.

    public static int Increment(ref int location);
    
    public static int Add(ref int location1, int value);

    Example:

    i = Interlocked.Add(ref j, 5);

    Here is the documentation of the method:

    https://docs.microsoft.com/en-US/dotnet/api/system.threading.interlocked.increment?view=netframework-4.7.2

    • Edited by DerChris88 Wednesday, January 16, 2019 8:38 PM
    • Marked as answer by Sudip_inn Thursday, January 17, 2019 3:36 PM
    Wednesday, January 16, 2019 8:33 PM
  • Hi Sudip_inn,

    Thank you for posting here.

    For your question, the Interlocked.Increment Method do something like number++ in the code. This method is the same as Add, but they use the value of 1 as an implicit argument. It could not be used to increase by 5.

    As DerChris88 said, you could use Add method to instead to increase with 5. Here is a simple example for your reference.

    Run the A method twice, it will return 10.

    class Program
        {
            static int _value;
            static void Main(string[] args)
            {
                Thread thread1 = new Thread(new ThreadStart(A));
                Thread thread2 = new Thread(new ThreadStart(A));
                thread1.Start();
                thread2.Start();
                thread1.Join();
                thread2.Join();
                Console.WriteLine(_value);
                Console.ReadKey();
            }
    
            static void A()
            {
                // Add 5.
                Interlocked.Add(ref _value, 5);
            }
        }

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Thursday, January 17, 2019 3:36 PM
    Thursday, January 17, 2019 7:07 AM
    Moderator