Csharp/C Sharp by API/System.Threading/Interlocked — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 15:31, 26 мая 2010

Interlocked.Add

  
using System;
using System.Threading;
class MainClass
{
    public static void Main()
    {
        int firstInt = 25;
        int secondInt = 80;
        Console.WriteLine("firstInt initial value = {0}", firstInt);
        Console.WriteLine("secondInt initial value = {0}", secondInt);
        Interlocked.Add(ref firstInt, secondInt);
        Console.WriteLine("firstInt after Add = {0}", firstInt);
        Console.WriteLine("secondInt after Add = {0}", secondInt);
    }
}


Interlocked.CompareExchange

  
using System;
using System.Threading;
class MainClass
{
    public static void Main()
    {
        int firstInt = 25;
        int secondInt = 80;
        Console.WriteLine("firstInt initial value = {0}", firstInt);
        Console.WriteLine("secondInt initial value = {0}", secondInt);
        Interlocked.rupareExchange(ref firstInt, 50, secondInt);
        Console.WriteLine("firstInt after CompareExchange = {0}", firstInt);
        Console.WriteLine("secondInt after CompareExchange = {0}", secondInt);
    }
}


Interlocked.Decrement

  
using System;
using System.Threading;
class MainClass
{
    public static void Main()
    {
        int firstInt = 25;
        int secondInt = 80;
        Console.WriteLine("firstInt initial value = {0}", firstInt);
        Console.WriteLine("secondInt initial value = {0}", secondInt);
        Interlocked.Decrement(ref firstInt);
        Console.WriteLine("firstInt after decrement = {0}", firstInt);

    }
}


Interlocked.Exchange

  
using System;
using System.Threading;
class MainClass
{
    public static void Main()
    {
        int firstInt = 25;
        int secondInt = 80;
        Console.WriteLine("firstInt initial value = {0}", firstInt);
        Console.WriteLine("secondInt initial value = {0}", secondInt);
        Interlocked.Exchange(ref secondInt, firstInt);
        Console.WriteLine("firstInt after Exchange = {0}", firstInt);
        Console.WriteLine("secondInt after Exchange = {0}", secondInt);
    }
}


Interlocked.Increment

  
using System;
using System.Threading;
class MainClass
{
    public static void Main()
    {
        int firstInt = 25;
        int secondInt = 80;
        Console.WriteLine("firstInt initial value = {0}", firstInt);
        Console.WriteLine("secondInt initial value = {0}", secondInt);
        Interlocked.Increment(ref secondInt);
        Console.WriteLine("secondInt after increment = {0}", secondInt);
    }
}