代码改变世界

.net 4.0新特性-自旋锁(SpinLock)

2010-06-10 10:21  Clingingboy  阅读(1129)  评论(1编辑  收藏  举报

概念:

http://baike.baidu.com/view/1250961.htm?fr=ala0_1_1

http://blog.csdn.net/wzhwho/archive/2009/05/15/4190090.aspx

参考:
http://msdn.microsoft.com/zh-cn/library/dd460716.aspx
http://www.cnblogs.com/lengleng3898/archive/2010/04/20/dotnet_4_parallel_plinq_2.html

http://www.cnblogs.com/prime/articles/1632670.html :) 很不错

msdn代码示例
比较了两种锁的效率
先用Monitor再决定是否用SpinLock

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;

class SpinLockDemo2
{
    const int N = 100000;
    static Queue<Data> _queue = new Queue<Data>();
    static object _lock = new Object();
    static SpinLock _spinlock = new SpinLock();

    class Data
    {
        public string Name { get; set; }
        public double Number { get; set; }
    }
    static void Main(string[] args)
    {

        // First use a standard lock for comparison purposes.
        UseLock();
        _queue.Clear();
        UseSpinLock();

        Console.WriteLine("Press a key");
        Console.ReadKey();

    }

    private static void UpdateWithSpinLock(Data d, int i)
    {
        bool lockTaken = false;
        try
        {
            _spinlock.Enter(ref lockTaken);
            _queue.Enqueue(d);
        }
        finally
        {
            if (lockTaken) _spinlock.Exit(false);
        }
    }

    private static void UseSpinLock()
    {

        Stopwatch sw = Stopwatch.StartNew();

        Parallel.Invoke(
                () =>
                {
                    for (int i = 0; i < N; i++)
                    {
                        UpdateWithSpinLock(new Data() { Name = i.ToString(), Number = i }, i);
                    }
                },
                () =>
                {
                    for (int i = 0; i < N; i++)
                    {
                        UpdateWithSpinLock(new Data() { Name = i.ToString(), Number = i }, i);
                    }
                }
            );
        sw.Stop();
        Console.WriteLine("elapsed ms with spinlock: {0}", sw.ElapsedMilliseconds);
    }

    static void UpdateWithLock(Data d, int i)
    {
        lock (_lock)
        {
            _queue.Enqueue(d);
        }
    }

    private static void UseLock()
    {
        Stopwatch sw = Stopwatch.StartNew();

        Parallel.Invoke(
                () =>
                {
                    for (int i = 0; i < N; i++)
                    {
                        UpdateWithLock(new Data() { Name = i.ToString(), Number = i }, i);
                    }
                },
                () =>
                {
                    for (int i = 0; i < N; i++)
                    {
                        UpdateWithLock(new Data() { Name = i.ToString(), Number = i }, i);
                    }
                }
            );
        sw.Stop();
        Console.WriteLine("elapsed ms with lock: {0}", sw.ElapsedMilliseconds);
    }
}

输出
在短时间占用资源的情况下,差别很大
image