Futex based locking mechanism

落爺英雄遲暮 提交于 2019-12-21 21:14:12

问题


Somebody can tell me an example of using locking mechanism based on futex? (for muticore x86 CPU, CentOS)


回答1:


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).




回答2:


Have a look at https://github.com/avsm/ipc-bench. They use futex in shared memory pipe implementation.

Specifically, you can check this code.




回答3:


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.



来源:https://stackoverflow.com/questions/3786947/futex-based-locking-mechanism

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!