fbpx
维基百科

自旋锁

自旋锁计算机科学用于多线程同步的一种,线程反复检查锁变量是否可用。由于线程在这一过程中保持执行,因此是一种忙等待。一旦获取了自旋锁,线程会一直保持该锁,直至显式释放自旋锁。

自旋锁避免了进程上下文的调度开销,因此对于线程只会阻塞很短时间的场合是有效的。因此操作系统的实现在很多地方往往用自旋锁。Windows操作系统提供的轻型读写锁(SRW Lock)内部就用了自旋锁。显然,单核CPU不适于使用自旋锁,这里的单核CPU指的是单核单线程的CPU,因为,在同一时间只有一个线程是处在运行状态,假设运行线程A发现无法获取锁,只能等待解锁,但因为A自身不挂起,所以那个持有锁的线程B没有办法进入运行状态,只能等到操作系统分给A的时间片用完,才能有机会被调度。这种情况下使用自旋锁的代价很高。

获取、释放自旋锁,实际上是读写自旋锁的存储内存或寄存器。因此这种读写操作必须是原子的。通常用test-and-set等原子操作来实现。[1]

Windows内核API 编辑

  • KeInitializeSpinLock //初始化自旋锁
  • KeAcquireSpinLock //获取自旋锁
  • KeReleaseSpinLock //释放自旋锁

例子 编辑

汇编语言写的代码,运行于Intel 80386兼容处理器。

; Intel syntax locked: ; The lock variable. 1 = locked, 0 = unlocked.  dd 0 spin_lock:  mov eax, 1 ; Set the EAX register to 1.  xchg eax, [locked] ; Atomically swap the EAX register with  ; the lock variable.  ; This will always store 1 to the lock, leaving  ; the previous value in the EAX register.  test eax, eax ; Test EAX with itself. Among other things, this will  ; set the processor's Zero Flag if EAX is 0.  ; If EAX is 0, then the lock was unlocked and  ; we just locked it.  ; Otherwise, EAX is 1 and we didn't acquire the lock.  jnz spin_lock ; Jump back to the MOV instruction if the Zero Flag is  ; not set; the lock was previously locked, and so  ; we need to spin until it becomes unlocked.  ret ; The lock has been acquired, return to the calling  ; function. spin_unlock:  mov eax, 0 ; Set the EAX register to 0.  xchg eax, [locked] ; Atomically swap the EAX register with  ; the lock variable.  ret ; The lock has been released. 

参见 编辑

参考文献 编辑

  1. ^ Silberschatz, Abraham; Galvin, Peter B. Operating System Concepts Fourth. Addison-Wesley. 1994: 176–179. ISBN 0-201-59292-4. 

外部链接 编辑

  • pthread_spin_lock documentation (页面存档备份,存于互联网档案馆) from The Open Group Base Specifications Issue 6, IEEE Std 1003.1, 2004 Edition
  • Variety of spinlock Implementations (页面存档备份,存于互联网档案馆) from Concurrency Kit
  • Article "" by Gert Boddaert
  • Paper "The Performance of Spin Lock Alternatives for Shared-Memory Multiprocessors (页面存档备份,存于互联网档案馆)" by Thomas E. Anderson
  • Paper "Algorithms for Scalable Synchronization on Shared-Memory Multiprocessors" by John M. Mellor-Crummey and Michael L. Scott. This paper received the 2006 Dijkstra Prize in Distributed Computing (页面存档备份,存于互联网档案馆).
  • Spin-Wait Lock (页面存档备份,存于互联网档案馆) by Jeffrey Richter
  • Austria C++ SpinLock Class Reference (页面存档备份,存于互联网档案馆
  • Interlocked Variable Access(Windows) (页面存档备份,存于互联网档案馆
  • Operating Systems: Three Easy Pieces (Chapter: Locks) (页面存档备份,存于互联网档案馆

自旋锁, 是计算机科学用于多线程同步的一种锁, 线程反复检查锁变量是否可用, 由于线程在这一过程中保持执行, 因此是一种忙等待, 一旦获取了, 线程会一直保持该锁, 直至显式释放, 避免了进程上下文的调度开销, 因此对于线程只会阻塞很短时间的场合是有效的, 因此操作系统的实现在很多地方往往用, windows操作系统提供的轻型读写锁, lock, 内部就用了, 显然, 单核cpu不适于使用, 这里的单核cpu指的是单核单线程的cpu, 因为, 在同一时间只有一个线程是处在运行状态, 假设运行线程a发现无法获取锁, . 自旋锁是计算机科学用于多线程同步的一种锁 线程反复检查锁变量是否可用 由于线程在这一过程中保持执行 因此是一种忙等待 一旦获取了自旋锁 线程会一直保持该锁 直至显式释放自旋锁 自旋锁避免了进程上下文的调度开销 因此对于线程只会阻塞很短时间的场合是有效的 因此操作系统的实现在很多地方往往用自旋锁 Windows操作系统提供的轻型读写锁 SRW Lock 内部就用了自旋锁 显然 单核CPU不适于使用自旋锁 这里的单核CPU指的是单核单线程的CPU 因为 在同一时间只有一个线程是处在运行状态 假设运行线程A发现无法获取锁 只能等待解锁 但因为A自身不挂起 所以那个持有锁的线程B没有办法进入运行状态 只能等到操作系统分给A的时间片用完 才能有机会被调度 这种情况下使用自旋锁的代价很高 获取 释放自旋锁 实际上是读写自旋锁的存储内存或寄存器 因此这种读写操作必须是原子的 通常用test and set等原子操作来实现 1 目录 1 Windows内核API 2 例子 3 参见 4 参考文献 5 外部链接Windows内核API 编辑KeInitializeSpinLock 初始化自旋锁 KeAcquireSpinLock 获取自旋锁 KeReleaseSpinLock 释放自旋锁例子 编辑汇编语言写的代码 运行于Intel 80386兼容处理器 Intel syntax locked The lock variable 1 locked 0 unlocked dd 0 spin lock mov eax 1 Set the EAX register to 1 xchg eax locked Atomically swap the EAX register with the lock variable This will always store 1 to the lock leaving the previous value in the EAX register test eax eax Test EAX with itself Among other things this will set the processor s Zero Flag if EAX is 0 If EAX is 0 then the lock was unlocked and we just locked it Otherwise EAX is 1 and we didn t acquire the lock jnz spin lock Jump back to the MOV instruction if the Zero Flag is not set the lock was previously locked and so we need to spin until it becomes unlocked ret The lock has been acquired return to the calling function spin unlock mov eax 0 Set the EAX register to 0 xchg eax locked Atomically swap the EAX register with the lock variable ret The lock has been released 参见 编辑忙碌等待 死锁 顺序锁 英语 Seqlock 排号自旋锁参考文献 编辑 Silberschatz Abraham Galvin Peter B Operating System Concepts Fourth Addison Wesley 1994 176 179 ISBN 0 201 59292 4 外部链接 编辑pthread spin lock documentation 页面存档备份 存于互联网档案馆 from The Open Group Base Specifications Issue 6 IEEE Std 1003 1 2004 Edition Variety of spinlock Implementations 页面存档备份 存于互联网档案馆 from Concurrency Kit Article User Level Spin Locks Threads Processes amp IPC by Gert Boddaert Paper The Performance of Spin Lock Alternatives for Shared Memory Multiprocessors 页面存档备份 存于互联网档案馆 by Thomas E Anderson Paper Algorithms for Scalable Synchronization on Shared Memory Multiprocessors by John M Mellor Crummey and Michael L Scott This paper received the 2006 Dijkstra Prize in Distributed Computing 页面存档备份 存于互联网档案馆 Spin Wait Lock 页面存档备份 存于互联网档案馆 by Jeffrey Richter Austria C SpinLock Class Reference 页面存档备份 存于互联网档案馆 Interlocked Variable Access Windows 页面存档备份 存于互联网档案馆 Operating Systems Three Easy Pieces Chapter Locks 页面存档备份 存于互联网档案馆 取自 https zh wikipedia org w index php title 自旋锁 amp oldid 72699396, 维基百科,wiki,书籍,书籍,图书馆,

文章

,阅读,下载,免费,免费下载,mp3,视频,mp4,3gp, jpg,jpeg,gif,png,图片,音乐,歌曲,电影,书籍,游戏,游戏。