Export (0) Print
Expand All

Interlocked.Decrement Method

Decrements a specified variable and stores the result, as an atomic operation.

Overload List

Decrements a specified variable and stores the result, as an atomic operation.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function Decrement(ByRef Integer) As Integer
[C#] public static int Decrement(ref int);
[C++] public: static int Decrement(int*);
[JScript] public static function Decrement(int) : int;

Decrements the specified variable and stores the result, as an atomic operation.

[Visual Basic] Overloads Public Shared Function Decrement(ByRef Long) As Long
[C#] public static long Decrement(ref long);
[C++] public: static __int64 Decrement(__int64*);
[JScript] public static function Decrement(long) : long;

Example

[Visual Basic, C#, C++] The following code example shows a thread-safe way to increment and decrement an integer value. SafeInstanceCount will always be zero. However, UnsafeInstanceCount will not necessarily be zero because a race condition occurs between incrementing and decrementing the count. This effect is especially marked on a multiprocessor computer.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of Decrement. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Imports Microsoft.VisualBasic
Imports System
Imports System.Threading

Public Class Test

    Shared Sub Main()
        Dim thread1 As New Thread(AddressOf ThreadMethod)
        Dim thread2 As New Thread(AddressOf ThreadMethod)
        thread1.Start()
        thread2.Start()
        thread1.Join()
        thread2.Join()

        ' Have the garbage collector run the finalizer for each
        ' instance of CountClass and wait for it to finish.
        GC.Collect()
        GC.WaitForPendingFinalizers()

        Console.WriteLine("UnsafeInstanceCount: {0}" & _
            vbCrLf & "SafeCountInstances: {1}", _
            CountClass.UnsafeInstanceCount.ToString(), _
            CountClass.SafeInstanceCount.ToString())
   End Sub

   Shared Sub ThreadMethod()
        Dim cClass As CountClass 

        ' Create 100,000 instances of CountClass.
        For i As Integer = 1 To 100000
            cClass = New CountClass()
            cClass = Nothing
            i += 1
        Next i
   End Sub

End Class

Public Class CountClass

    Shared unsafeCount As Integer = 0
    Shared   safeCount As Integer = 0

    Shared ReadOnly Property UnsafeInstanceCount As Integer
        Get
            Return unsafeCount
        End Get
    End Property

    Shared ReadOnly Property SafeInstanceCount As Integer
        Get
            Return safeCount
        End Get
    End Property
   
    Sub New()
        unsafeCount += 1
        Interlocked.Increment(safeCount)
    End Sub

    Protected Overrides Sub Finalize()
        unsafeCount -= 1
        Interlocked.Decrement(safeCount)
        MyBase.Finalize()
    End Sub

End Class

[C#] 
using System;
using System.Threading;

class Test
{
    static void Main()
    {
        Thread thread1 = new Thread(new ThreadStart(ThreadMethod));
        Thread thread2 = new Thread(new ThreadStart(ThreadMethod));
        thread1.Start();
        thread2.Start();
        thread1.Join();
        thread2.Join();

        // Have the garbage collector run the finalizer for each
        // instance of CountClass and wait for it to finish.
        GC.Collect();
        GC.WaitForPendingFinalizers();

        Console.WriteLine("UnsafeInstanceCount: {0}" +
            "\nSafeCountInstances: {1}",
            CountClass.UnsafeInstanceCount.ToString(),
            CountClass.SafeInstanceCount.ToString());
    }

    static void ThreadMethod()
    {
        CountClass cClass;
        
        // Create 100,000 instances of CountClass.
        for(int i = 0; i < 100000; i++)
        {
            cClass = new CountClass();
        }
    }
}

class CountClass
{
    static int unsafeInstanceCount = 0;
    static int   safeInstanceCount = 0;

    static public int UnsafeInstanceCount
    {
        get {return unsafeInstanceCount;}
    }

    static public int SafeInstanceCount
    {
        get {return safeInstanceCount;}
    }

    public CountClass()
    {
        unsafeInstanceCount++;
        Interlocked.Increment(ref safeInstanceCount);
    }

    ~CountClass()
    {
        unsafeInstanceCount--;
        Interlocked.Decrement(ref safeInstanceCount);
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Threading;

__gc class CountClass
{
    static int unsafeInstanceCount;
    static int safeInstanceCount;

public:
    __property static int get_UnsafeInstanceCount()
    {
       return unsafeInstanceCount;
    }

    __property static int get_SafeInstanceCount()
    {
        return safeInstanceCount;
    }

    CountClass()
    {
        unsafeInstanceCount++;
        Interlocked::Increment(&safeInstanceCount);
    }

    ~CountClass()
    {
        unsafeInstanceCount--;
        Interlocked::Decrement(&safeInstanceCount);
    }
};

__gc class Test
{
public:
    static void ThreadMethod()
    {
        CountClass* cClass;

        // Create 100,000 instances of CountClass.
        for(int i = 0; i < 100000; i++)
        {
            cClass = new CountClass();
        }
    }
};

void main()
{
    Thread* thread1 = 
        new Thread(new ThreadStart(0, &Test::ThreadMethod));
    Thread* thread2 = 
        new Thread(new ThreadStart(0, &Test::ThreadMethod));
    thread1->Start();
    thread2->Start();
    thread1->Join();
    thread2->Join();

    // Have the garbage collector run the finalizer for each
    // instance of CountClass and wait for it to finish.
    GC::Collect();
    GC::WaitForPendingFinalizers();

    Console::WriteLine(S"UnsafeInstanceCount: {0}"
        S"\nSafeCountInstances: {1}",
        CountClass::UnsafeInstanceCount.ToString(),
        CountClass::SafeInstanceCount.ToString());
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

Interlocked Class | Interlocked Members | System.Threading Namespace

Show:
© 2014 Microsoft