Futex based locking mechanism

假装没事ソ 提交于 2019-12-04 17:41:07

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

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

Specifically, you can check this code.

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.

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