views:

65

answers:

1

Hello,

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

Thanks Dima

+3  A: 

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

Doug
What about latest version of CentOS 5.5? Is Pthreads' mutexes on CentOS 5.5 are implemented using futexes?Thanks
Dima
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
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
@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