简体   繁体   English

Linux中最低的锁定原语是什么

[英]What are the lowest locking primitives in Linux

What are the lowest locking primitives in linux. linux中最低的锁定原语是什么? I know about futex_wait and futex_wake . 我知道futex_waitfutex_wake But futexes can be used for signalling besides locking. 但是除了锁定之外,futex还可以用于信令。 What is the lowest locking primitive and where can I find it in the glibc library? 什么是最低的锁定原语,我在哪里可以在glibc库中找到它? Secondly, are the condition variables also based on futexes? 其次,条件变量是否也基于互斥量?

Read various documentations on futex -es, including futex(2) and futex(7) man pages. 阅读有关futex -es的各种文档,包括futex(2)futex(7)手册页。 You'll learn that pthread locking primitives are implemented with a mixture of futexes and of assembly code (doing things non-expressible in portable C99) 您将了解到pthread锁定原语是通过futexes和汇编代码的混合实现的(在便携式C99中不可表达的事情)

(as I said in a comment) (正如我在评论中所说)

There is no such thing as "lowest", but locks for different scenarios and purposes and "signalling" is not the right criterion to distinguish different sorts of locks. 没有“最低”的东西,但是针对不同场景和目的的锁定以及“信令”不是区分不同种类的锁的正确标准。

  • There are locking primitives that preform an active wait, spinlocks, that are based on atomic operations. 有一些锁定原语可以执行基于原子操作的活动等待,自旋锁。 With the venue of C11, these aren't even OS features anymore, atomic_flag can be used for that. 在C11的场地,这些甚至不再是OS功能, atomic_flag可以用于此。
  • The other family of lock primitives suspend the calling thread during wait. 另一类锁原语在等待期间暂停调用线程。 In linux these are all implemented with a combination of atomic operations and futex as work horse underneath for the waiting part. 在linux中,这些都是通过原子操作和futex的组合实现的,作为等待部分的工作马。

Spinlocks are best used in contexts where you know that the critical phase only a handful of assembler instructions and where an interruption of the critical phase by the scheduler or signals is unlikely. 自旋锁最适用于您知道关键阶段只有少数汇编指令以及调度程序或信号不可能中断临界阶段的情况。 This is the case for much more contexts than many people tend to belief. 与许多人倾向于相信的情况相比,情况更多。 Therefore the atomic_flag is an important new feature in the C language. 因此, atomic_flag是C语言中一个重要的新功能。

The other locks are best used in context where active wait would eat up a substantial part of system resources. 其他锁最好用于主动等待占用大部分系统资源的环境中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM