基于thread.spinwait实现自旋锁

实现原理:基于test–and–set原子操作实现
使用一个数据表示当前锁是否已经被获取 0表示未被索取,1表示已经获取 获取锁时会将_lock的值设置为1 然后检查修改前的值是否等于0,

优点:

  • 不使用thread.spinwait方法,重试的方法体会为空,cpu会使用它的最大性能来不断的进行赋值和比较指令,会浪费很大的性能,thread.spinwait提示cpu当前正在自旋锁的循环中,可以休息若干个时间周期
  • 使用自旋锁需要注意的问题,自旋锁保护的代码应该在非常短的时间内执行完成,如果时间过长,其他线程不断重试导致影响其他线程进行

缺点:

当前实现没有考虑到公平性,如果多个线程同时获取锁失败,按时间顺序第一个获取锁的线程不一定会在释放锁后第一个获取成功,

代码实现:

public static class threadspinwaitdemo
    {
        private static int _lock = 0;
        private static int _countera = 0;
        private static int _counterb = 0;

        public static void incrementcounters()
        {
            while (interlocked.exchange(ref _lock, 1) != 0)
            {
                thread.spinwait(1);
            }

            ++_countera;
            ++_counterb;
            interlocked.exchange(ref _lock, 0);
        }

        public static void getcounters(out int countera, out int counterb)
        {
            while (interlocked.exchange(ref _lock, 1) != 0)
            {
                thread.spinwait(1);
            }
            countera = _countera;
            counterb = _counterb;
            interlocked.exchange(ref _lock, 0);

        }
    }

基于spinwaite实现自旋锁

特性是spinonce方法的次数,如果在一定次数以内并且当前逻辑核心所大于1,则调用thread.spinwait函数;如果超过一定次数或者当前环境逻辑核心数等于1,则交替使用
thread.sleep(0)和thread.yield函数,表示切换到其他线程,如果再超过一定次数,则让当前线程休眠
spinwaite解决thread.spinwait中的两个问题

  • 如果自旋锁运行时间超长,spinwaite可以提示操作系统切换到其他线程或者让当前线程进入休眠状态,
  • 如果当前环境只有一个核心逻辑,spinwaite不会执行thread.spinwait函数,而是直接提示操作系统切换到其他线程,
public static class threadspinoncedemo
    {
        private static int _lock = 0;
        private static int _countera = 0;
        private static int _counterb = 0;


        public static void incrementcounters()
        {
            var spinwait = new spinwait();
            while (interlocked.exchange(ref _lock, 1) != 0)
            {
                spinwait.spinonce();
            }

            ++_countera;
            ++_counterb;
            interlocked.exchange(ref _lock, 0);
        }

        public static void getcounters(out int countera, out int counterb)
        {
            var spinwait = new spinwait();
            while (interlocked.exchange(ref _lock, 1) != 0)
            {
                spinwait.spinonce();
            }
            countera = _countera;
            counterb = _counterb;
            interlocked.exchange(ref _lock, 0);

        }
    }

基于spinlock实现自旋锁

封装了spinwaite的逻辑

spinlock代码实现

public class threadspinlockdemo
    {
        private static spinlock _spinlock = new spinlock();
        private static int _countera = 0;
        private static int _counterb = 0;

        public static void incrementcounters()
        {
            bool locktaken = false;
            try
            {
                _spinlock.enter(ref locktaken);
                ++_countera;
                ++_counterb;
            }
            finally
            {
                if (locktaken)
                {
                    _spinlock.exit();
                }
            }
        }

        public static void getcounters(out int countera, out int counterb)
        {
            bool locktaken = false;
            try
            {
                _spinlock.enter(ref locktaken);
                countera = _countera;
                counterb = _counterb;
            }
            finally
            {
                if (locktaken)
                {
                    _spinlock.exit();
                }
            }
        }
    }

简述 thread.sleep(0)和thread.yield的区别

  • 在windows系统中 thread.sleep调用系统提供的sleepex函数,thread.yield函数调用的是系统提供的switchtothread方法,
  • 区别在于switchtothread函数只会切换到当前核心逻辑关联的待运行队列的线程,不会切换到其他核心逻辑关联的线程上,而sleepex函数会切换到任意逻辑核心关联的待运行队列中的线程,并且让当前线程在指定时间内无法重新进入待运行队列(如果线程为0 那么线程可以立刻重新进入待运行队列)
  • 在linux和osx中 thread.sleep函数在休眠时间不为0时会调用pthread类库提供的pthread_cond_timedwait函数,在休眠时间为0时会调用sched_yield函数,thread.yield同样会调用sched_yield函数 sched_yield在windows和osx系统中没有区别,都只会切换到当前和逻辑核心关心的待运行队列中的线程,不会切换到其他核心逻辑关联的线程上。在unix系统上调用系统提供的sleep函数并传入0 会直接忽略返回

以上就是.net基于thread实现自旋锁的三种方式的详细内容,更多关于.net自旋锁的资料请关注www.887551.com其它相关文章!