基于Futex的锁定机制
有人可以告诉我一个使用基于futex的锁定机制的例子吗? (适用于多核 x86 CPU、CentOS)
Somebody can tell me an example of using locking mechanism based on futex? (for muticore x86 CPU, CentOS)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Pthreads 的互斥体是在最新版本的 Linux 上使用 futexes 实现的。 Pthreads 是 Linux 上的标准 C 线程 API,并且是Posix 标准,因此您可以轻松地将程序移植到其他类 Unix 系统。除非您有非常不寻常的需求,否则您应该避免直接使用 futexes,因为它们很难正确使用 - 使用 pthreads 或更高级别的特定于语言的 API(几乎肯定会使用 pthreads 本身)。
Pthreads' mutexes are implemented using futexes on recent versions of Linux. Pthreads is the standard C threading API on Linux, and is part of the Posix standard, so you can easily port your program to other Unix-like systems. You should avoid using futexes directly unless you have very unusual needs, because they're very hard to use correctly - use pthreads, or a higher-level, language-specific API (which will almost certainly use pthreads itself).
看看https://github.com/avsm/ipc-bench。他们在共享内存管道实现中使用 futex。
具体可以查看这段代码。
Have a look at https://github.com/avsm/ipc-bench. They use futex in shared memory pipe implementation.
Specifically, you can check this code.
工作示例:pthreads 互斥体使用 futex 锁。
代码示例:这些是在 10 年这篇文章发表后的几个月内制作的,但仍然是最新的。
http://meta-meta.blogspot.com/2010 /11/linux-threading-primitives-futex.html
https://github.com/lcapaldo/futexexamples
用例示例:IPC 和进程间同步是这是为什么应该在用户空间使用 futex 的唯一例子。除极端情况外,pthread 互斥体适用于多线程,但多进程缺乏高性能的锁定机制和锁定类型。
working example: pthreads mutex use futex locks.
code example: These were made within months of this post in '10 but are still up-to-date.
http://meta-meta.blogspot.com/2010/11/linux-threading-primitives-futex.html
https://github.com/lcapaldo/futexexamples
use case example: IPC and inter-process synchronization are the only example of why one should use a futex in userspace. pthread mutexes will work for multi-thread except for extreme cases, but multi-process is lacking in high performance locking mechanisms as well as lock types.