(精华)2021年02月19日 .NET Core 多线程底层详解(原子操作)

一:原则操作的概念

所谓原子操作是指不会被线程调度机制打断的操作;这种操作一旦开始,就一直运行到结束,中间不会有任何 context switch
(切换到另一个线程)。

原子操作案例

//Interlocked为原子操作相关类,a值为最终值,b可能因为同时操作返回不同的值
public static class AtomicSaple
{
    public static int a = 0;

    public static void Sample()
    {
        var b = 0;
        b = Interlocked.Increment(ref a);
        Console.WriteLine(b);

        b = Interlocked.Decrement(ref a);
        Console.WriteLine(b);

        b = Interlocked.Add(ref a, 2);
        Console.WriteLine(b);
		//a的值改变为1
        Interlocked.Exchange(ref a, 1);
		//如果a=0返回1
        Interlocked.CompareExchange(ref a,1,0);
    }
}

原子操作实现无锁算法

public class Counter
{
    public int TimesA { get; set; }
    public int TimesB { get; set; }
    public Counter(int timesA, int timesB)
    {
        TimesA = timesA;
        TimesB = timesB;
    }
}

public static class CounterSample
{
    public static Counter Counter1 = new Counter(0, 0);
    /// <summary>
    /// 递增计数器,返回递增后的值
    /// </summary>
    /// <param name="counter"></param>
    /// <returns></returns>
    public static Counter Increment(ref Counter counter)
    {
        Counter oldCounter, newCounter;
        do
        {
            oldCounter = counter;
            newCounter = new Counter(oldCounter.TimesA + 1, oldCounter.TimesB + 1);

            // 使用循环确保成功
        } while (Interlocked.CompareExchange(ref counter, newCounter,oldCounter) != oldCounter);

        return newCounter; 
    }

    public static void IncrementCounter()
    {
        var result = Increment(ref Counter1);
        Console.WriteLine($"TimesA={result.TimesA},TimesB={result.TimesB}");
    }

    public static void ThreadIncrement()
    {
        var thread1 = new Thread(IncrementCounter);
        var thread2 = new Thread(IncrementCounter);

        thread1.Start();
        thread2.Start();
    }

    public static void PrintCounter()
    {
        var c = Counter1;
        Console.WriteLine($"Counter={c.TimesA},{c.TimesB}");
    }

    public static void Run()
    {
        ThreadIncrement();
        Thread.Sleep(1000);
        PrintCounter();
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

愚公搬代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值