博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
How to: Use SpinLock for Low-Level Synchronization
阅读量:6408 次
发布时间:2019-06-23

本文共 3931 字,大约阅读时间需要 13 分钟。

hot3.png

跑了一下下面的例子,发现在100000级别,SpinLock比对象lock要快不少,但是随着N的增大,效率差异基本没有,有时甚至更加慢。

In this example, the critical section performs a minimal amount of work, which makes it a good candidate for a . Increasing the work a small amount increases the performance of the compared to a standard lock. However, there is a point at which a SpinLock becomes more expensive than a standard lock. You can use the concurrency profiling functionality in the profiling tools to see which type of lock provides better performance in your program. For more information, see .

class SpinLockDemo2{            const int N = 100000;    static Queue _queue = new Queue();    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);    }}

might be useful when a lock on a shared resource is not going to be held for very long. In such cases, on multi-core computers it can 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. will stop spinning under certain conditions to prevent starvation of logical processors or priority inversion on systems with Hyper-Threading.

This example uses the class, which requires user synchronization for multi-threaded access. In applications that target the .NET Framework version 4, another option is to use the , which does not require any user locks.

Note the use of false (False in Visual Basic) in the call to . This provides the best performance. Specify true (True)on IA64 architectures to use the memory fence, which flushes the write buffers to ensure that the lock is now available for other threads to exit.

转载于:https://my.oschina.net/u/138995/blog/181781

你可能感兴趣的文章
HttpHandler初探 - 页面上输出图像
查看>>
框架和语言的作用
查看>>
unidac连接ORACLE免装客户端驱动
查看>>
Cygwin + OpenSSH FOR Windows的安装配置
查看>>
咏南中间件支持手机客户端
查看>>
fastscript增加三方控件之二
查看>>
Windows Vista RTM 你准备好了么?
查看>>
Tensorflow Serving 模型部署和服务
查看>>
Java Web开发详解——XML+DTD+XML Schema+XSLT+Servlet 3.0+JSP 2.2深入剖析与实例应用
查看>>
topcoder srm 680 div1 -3
查看>>
topcoder srm 430 div1
查看>>
具体数学第二版第四章习题(1)
查看>>
高效前端优化工具--Fiddler入门教程
查看>>
【翻译】我钟爱的HTML5和CSS3在线工具
查看>>
Java多线程学习(吐血超详细总结)
查看>>
css3 变形
查看>>
[发布] 电量监视+自动关机工具
查看>>
POJ 1696 Space Ant(极角排序)
查看>>
关于datawindow does not have update capability
查看>>
Win7 64bit 安装Mysql5 出错 无法启动服务。
查看>>