Somebody can tell me an example of using locking mechanism based on futex? (for muticore x86 CPU, CentOS)
3 Answers
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).

- 8,780
- 2
- 27
- 37
-
What about latest version of CentOS 5.5? Is Pthreads' mutexes on CentOS 5.5 are implemented using futexes? Thanks – Dima Sep 24 '10 at 14:53
-
Futexes have been used by pthreads on all distributions of Linux since about 2004, when 2.6-series kernels were adopted. That includes CentOS. I don't understand why you need to worry about them, though... they're largely an implementation detail. – Doug Sep 24 '10 at 15:51
-
because I need to use locks in very time-critical path of the server application (the place where all data passed through). I think that is better to use some "fast" locking mechanism... – Dima Sep 24 '10 at 19:23
-
5@Dima: You shouldn't assume that pthreads mutexes are too slow until you've tried them. And futexes aren't really a complete locking mechanism, they're just the kernel part of it. You still need to write some assembly language in order to use them as a lock, which is very, very hard to get right. I recommend writing your program with pthreads mutexes, which actually are very fast. If your program is too slow, then try making the locks finer-grained, so that different threads don't need all need to use the same lock at the same time. Although this sounds hard, it's easier than using futexes. – Doug Sep 25 '10 at 02:05
-
3@Doug - I know this is an older post, but futex has never required asm from the best I can tell. http://people.redhat.com/drepper/futex.pdf was writen in ,04 and didn't even call for asm at that point when they were still relativly new. – JSON Dec 13 '13 at 02:40
-
And generaly speaking, the ability to code a futex is a matter of compatancy in C. If one able to do multi threaded pthreads coding they should be assumed capable of using futex. Really. – JSON Dec 13 '13 at 02:49
-
3All due respect, "you should not use it" is not a real answer to "how to use something". Not to mention you exaggerated the difficulty to use futex. – xiay May 04 '18 at 01:59
Have a look at https://github.com/avsm/ipc-bench. They use futex in shared memory pipe implementation.
Specifically, you can check this code.

- 8,064
- 6
- 45
- 62
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.

- 1,819
- 20
- 27