自旋锁,如何有用的是他们? [英] Spinlocks, How Useful Are They?

查看:167
本文介绍了自旋锁,如何有用的是他们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你经常发现自己居然在代码中使用自旋锁?如何常见的是它遇到的情况是使用一个繁忙的循环实际上优于锁的使用情况如何?

就个人而言,当我写一些代码,需要线程安全,我倾向于基准它与不同同步原语,并尽可能去,好像使用锁提供了比使用自旋锁更好的性能。对我怎么一点时间实际持有锁不管,我使用自旋锁时收到争的量远远大于我使用的锁获取量大(当然,我在多处理器机器上运行我的测试)。

How often do you find yourself actually using spinlocks in your code? How common is it to come across a situation where using a busy loop actually outperforms the usage of locks?
Personally, when I write some sort of code that requires thread safety, I tend to benchmark it with different synchronization primitives, and as far as it goes, it seems like using locks gives better performance than using spinlocks. No matter for how little time I actually hold the lock, the amount of contention I receive when using spinlocks is far greater than the amount I get from using locks (of course, I run my tests on a multiprocessor machine).

我认识到,它更可能在低层次的代码,遇到一个自旋锁,但我想知道你是否发现它甚至更有用高层次的一种编程的?

I realize that it's more likely to come across a spinlock in "low-level" code, but I'm interested to know whether you find it useful in even a more high-level kind of programming?

推荐答案

这取决于你在做什么。在一般的应用程序代码中,你要避免自旋锁。

It depends on what you're doing. In general application code, you'll want to avoid spinlocks.

在低层次的东西在这里您将只持有一对夫妇的指示锁,并且延迟重要的是,一个自旋锁垫比锁更好的解决方案。但这些情况是罕见的,特别是在那种其中C#,通常使用的应用程序。

In low-level stuff where you'll only hold the lock for a couple of instructions, and latency is important, a spinlock mat be a better solution than a lock. But those cases are rare, especially in the kind of applications where C# is typically used.

这篇关于自旋锁,如何有用的是他们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆