自旋锁在单处理器单核体系结构中有用吗? [英] Is spin lock useful in a single processor uni core architecture?

查看:422
本文介绍了自旋锁在单处理器单核体系结构中有用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对自旋锁的功能感到困惑.

I am confused by the function of spin lock.

自旋锁用于阻止进程重新调度. 但是,在一台只有一个内核的机器中,使用自旋锁是否有用 防止上下文切换?

The spin lock is used to stop the process from re-scheduling. However, in a machine with just one core, is it useful to use spin lock to prevent context switching?

推荐答案

简短答案:否.

根据 http://uw714doc. sco.com/en/man/html.3synch/Intro.3synch.html

According to http://uw714doc.sco.com/en/man/html.3synch/Intro.3synch.html

自旋锁不得在单处理器系统上使用.最好的情况是,单个处理器系统上的自旋锁会浪费资源,从而减慢锁的所有者.在最坏的情况下,它将使处理器死锁.

Spin locks must not be used on a single processor system. In the best case, a spin lock on a single processor system will waste resources, slowing down the owner of the lock; in the worst case, it will deadlock the processor.

来自:

From: http://blogs.microsoft.co.il/blogs/sasha/archive/2008/08/10/practical-concurrency-patterns-spinlock.aspx

在单处理器系统上,不需要自旋锁,因为仅在高IRQL上需要自旋锁同步.在较高的IRQL(高于调度IRQL)上,不会发生上下文切换,因此,除了旋转获取线程之外,还可以简单地在相关的IRQL上请求中断并返回;该中断将被屏蔽,直到释放线程将IRQL降低到所请求的IRQL以下为止.

On single-processor systems, spinlocks are not needed because spinlock synchronization is required on high IRQLs only. On high IRQLs (above dispatch IRQL) a context switch cannot occur, so instead of spinning the acquiring thread can simply request an interrupt on the relevant IRQL and return; the interrupt will be masked until the releasing thread lowers the IRQL below the requested IRQL.

对于单处理器系统,内核将忽略旋转计数值,并将其视为零-本质上将旋转锁设为无操作.

For single processor systems, the kernel will ignore the spin count value, and treat it as zero - essentially making a spinlock a no-op.

是的,自旋锁可能有用,并且可以提高某些操作的效率.但是,通常应从互斥锁开始,如果分析表明它是瓶颈,则可能需要考虑使用自旋锁.

Yes, spin locks can be useful, and improve efficiency of some operations. However, generally you should start with a mutex, and if profiling show it to be a bottleneck, you may want to consider a spinlock.

这篇关于自旋锁在单处理器单核体系结构中有用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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