سؤال

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

هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top