Interlocked.Decrement メソッド
分割不可能な操作として、指定した変数をデクリメントし、結果を格納します。
オーバーロードの一覧
分割不可能な操作として、指定した変数をデクリメントし、結果を格納します。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Shared Function Decrement(ByRef Integer) As Integer
分割不可能な操作として、指定した変数をデクリメントしてその結果を格納します。
[Visual Basic] Overloads Public Shared Function Decrement(ByRef Long) As Long
使用例
[Visual Basic, C#, C++] 整数値をインクリメントおよびデクリメントするスレッド セーフな方法の例を次に示します。 SafeInstanceCount は常に 0 になります。ただし、 UnsafeInstanceCount は、カウントのインクリメントとデクリメントの間で競合条件が発生するために、必ずしも 0 になるとは限りません。この現象は、マルチプロセッサ コンピュータで特に顕著です。
[Visual Basic, C#, C++] メモ ここでは、Decrement のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。
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] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。