Spinlock

Spinlock

From Wikipedia, the free encyclopedia


This article needs additional citations for verification. Please help improve this article by adding citations to reliable sources. Unsourced material may be challenged and removed. (October 2012)

In software engineering, a spinlock is a lock which causes a thread trying to acquire it to simply wait in a loop ("spin") while repeatedly checking if the lock is available. Since the thread remains active but is not performing a useful task, the use of such a lock is a kind of busy waiting. Once acquired, spinlocks will usually be held until they are explicitly released, although in some implementations they may be automatically released if the thread being waited on (that which holds the lock) blocks, or "goes to sleep".

Because they avoid overhead from operating system process rescheduling or context switching, spinlocks are efficient if threads are likely to be blocked for only short periods. For this reason, operating-system kernels often use spinlocks. However, spinlocks become wasteful if held for longer durations, as they may prevent other threads from running and require rescheduling. The longer a thread holds a lock, the greater the risk that the thread will be interrupted by the OS scheduler while holding the lock. If this happens, other threads will be left "spinning" (repeatedly trying to acquire the lock), while the thread holding the lock is not making progress towards releasing it. The result is an indefinite postponement until the thread holding the lock can finish and release it. This is especially true on a single-processor system, where each waiting thread of the same priority is likely to waste its quantum (allocated time where a thread can run) spinning until the thread that holds the lock is finally finished.

Implementing spin locks correctly offers challenges because programmers must take into account the possibility of simultaneous access to the lock, which could cause race conditions. Generally, such implementation is possible only with special assembly-language instructions, such as atomic test-and-set operations, and cannot be easily implemented in programming languages not supporting truly atomic operations.[1] On architectures without such operations, or if high-level language implementation is required, a non-atomic locking algorithm may be used, e.g. Peterson‘s algorithm. But note that such an implementation may require more memory than a spinlock, be slower to allow progress after unlocking, and may not be implementable in a high-level language if out-of-order execution is allowed.

时间: 2024-08-26 07:21:18

Spinlock的相关文章

JOS中 "spinlock" 的实现

JOS中  "spinlock" 的实现 In software engineering, a spinlock is a lock which causes a thread trying to acquire it to simply wait in a loop ("spin") while repeatedly checking if the lock is available. Since the thread remains active but is

A multiprocessing system including an apparatus for optimizing spin-lock operations

A multiprocessing system having a plurality of processing nodes interconnected by an interconnect network. To optimize performance during spin-lock operations, a home agent prioritizes the servicing of read-to-own (RTO) transaction requests over the

[20140829]spinlock导致cpu居高不下

背景: 出现cpu高于常规的告警 排查: 1.开跟踪,没有发现cup特别高的查询 2.查看内核cpu使用量,看是否是sql server 端引起 3.查看负荷,是否负荷特别高这里使用 batch request 4.全部无解,sql运行都正常,在thread,worker,task级别查看也未发现有问题 5.开procexp.exe希望可以找到足丝马迹,点击进去发现,所有高cpu使用的线程都在KeAcquireSpinLockAtDpcLevel  调用下,开始怀疑是否是spinlock问题.

SpinLock 自旋锁, CAS操作(Compare & Set) ABA Problem

SpinLock 自旋锁 spinlock 用于CPU同步, 它的实现是基于CPU锁定数据总线的指令. 当某个CPU锁住数据总线后, 它读一个内存单元(spinlock_t)来判断这个spinlock 是否已经被别的CPU锁住. 如果否, 它写进一个特定值, 表示锁定成功, 然后返回. 如果是, 它会重复以上操作直到成功, 或者spin次数超过一个设定值. 锁定数据总线的指令只能保证一个机器指令内, CPU独占数据总线. 单CPU当然能用spinlock, 但实现上无需锁定数据总线. spinl

Samsung_tiny4412(笔记)-->spinlock,semaphore,atomic,mutex,completion,interrupt

/*********************************************************************************** * * Samsung_tiny4412(笔记)-->spinlock,semaphore,atomic,mutex,completion,interrupt * * 声明: * 1. 本文中有些源代码没有全部帖出来,主要是因为篇幅太大的原因; * 2. 基于1中的原因,本文借鉴了python中的缩进代码风格进行代码的体现: *

Windows Driver—自旋锁(SpinLock)

http://hgy413.com/1335.html 简介自旋锁的注意点和自旋锁实现原型代码 Windows Driver-自旋锁(SpinLock),布布扣,bubuko.com

spinlock,mutex,semaphore,critical section的作用与差别

某年深信服的笔试题,考的就是多线程的同步.简单的解释下方便记忆: 1.spinlock:自旋锁.是专为防止多处理器并发而引入的一种锁. 2.mutex:相互排斥量. 仅仅有拥有相互排斥对象的线程才有訪问公共资源的权限.保证了资源不会同一时候被多个线程訪问. 3.semaphore:信号量.同意多个线程同一时候訪问资源,限制訪问资源的最大线程数. 4.critical section:临界区. 随意时刻仅仅同意一个线程对共享资源进行訪问.

atomic, spinlock and mutex性能比较

我非常好奇于不同同步原理的性能,于是对atomic, spinlock和mutex做了如下实验来比较: 1. 无同步的情况 1 #include <future> 2 #include <iostream> 3 4 volatile int value = 0; 5 6 int loop (bool inc, int limit) { 7 std::cout << "Started " << inc << " &qu

spinlock原理

[参考] http://www.searchtb.com/2011/06/spinlock%E5%89%96%E6%9E%90%E4%B8%8E%E6%94%B9%E8%BF%9B.html