• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

c#spinLock和spinWait基本使用

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

关于spinlock的基本介绍;

https://docs.microsoft.com/en-us/dotnet/standard/threading/spinlock

基本使用实例:

http://www.c-sharpcorner.com/UploadFile/1d42da/spinlock-class-in-threading-C-Sharp/

这个也是有一点难理解的呀我操;

https://msdn.microsoft.com/zh-tw/library/ee722114.aspx

https://msdn.microsoft.com/zh-tw/library/ee722116.aspx

 

spinwait 和 sleep的基本比较:https://www.codeproject.com/Questions/874791/Thread-Sleep-vs-Thread-SpinWait

 

后面我们再深入的理解一哈,先make

这里再收集一篇,关于多线程中random的使用:http://blog.csdn.net/xxdddail/article/details/16980743

关于spainWait 和 sleep 之间的一个简单比较:

http://blog.csdn.net/xxdddail/article/details/16982351

还有一篇,深度讲解额文章:

http://geekswithblogs.net/akraus1/archive/2015/06/18/165219.aspx

http://www.codeguru.com/csharp/article.php/c19283/SpinWaitSpinUntil-and-the-Task-Parallel-Library.htm

 

关于spinlock的基本使用法如下

var spinLock = new SpinLock (true);   // Enable owner tracking
bool lockTaken = false;
try
{
  spinLock.Enter (ref lockTaken);
  // Do stuff...
}
finally
{
  if (lockTaken) spinLock.Exit();
}

然后这个,lockTaken的由来是来自这里的啊;

 

spinlock 和 lock的一个对比; 在这种场景下,spanlock 花费的时间似乎更长一些; 我们后面再考虑更好测场景;

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

namespace ConsoleApplication41
{
    class Program
    {
        //来自微软官方的实例代码;
        //To avoid this danger, CLR 4.0’s designers added the following overload to Monitor.Enter:
        //however, if you expect a significant amount of blocking, you should probably not use spin locks due to excessive spinning. Spinning can be beneficial when locks are fine-grained 


        const int N = 100000;
        static Queue<Data> _queue = new Queue<Data>();
        static object _lock = new Object();   //a shared resource is not going to be held for very long
        static  SpinLock _spinlock = new SpinLock(); //Do not store SpinLock instances in readonly fields

        //be efficient for the blocked thread to spin for a few cycles until the lock is released. By spinning
        //the thread does not become blocked, which is a CPU-intensive process
        //SpinLock should only be used after you have been determined that doing so will improve an application's performance
        //tracking thread owner


        class Data
        {
            public string Name { get; set; }
            public double Number { get; set; }
        }

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

        private static void UpdateWithSpinLock(Data d, int i)
        {
            bool lockTaken = false; //lockTaken is false after this method if (and only if) the Enter method throws an exception and the lock was not taken
            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);
        }

        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);

        }


        static void Main(string[] args)
        {

            UseLock();
            _queue.Clear();
            UseSpinLock();  //用spanlock似乎话费的较长的时间;

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

        }
    }
}

大致的结果如下:

所以,不得不说明,这是一个low-level 的应用场景滴呀;

 

然后我们来看另外的实例场景的使用滴呀;(信息来源:https://msdn.microsoft.com/en-us/library/system.threading.spinlock(v=vs.110).aspx)

 

https://docs.microsoft.com/en-us/dotnet/standard/threading/spinlock

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
第二章:深入C#数据类型发布时间:2022-07-10
下一篇:
C#委托的发展史:.Net1.x委托=.Net2.x匿名方法=.Net3.0/3.5Lambda表达式 ...发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap