Please be more precise about what you are looking for. As can be read in th wikipedia article about critical section, the application-level (user-space) critical section under linux uses mutex.
–
rozcietrzewiaczJan 16 '12 at 10:08

3

"it must be faster than a mutex" is not generally true, depends on how the mutex is implemented. Mutex implementations on Windows and Linux (pthreads) are potentially very different.
–
MatJan 16 '12 at 12:12

thank you. I thought the implementations of mutex in windows and linux are similar, because implementing it is widely known.
–
Hyunjik BaeJan 17 '12 at 7:18

2 Answers
2

If you want something coming from kernel space, then you might want to look at semaphores (sem_overview(7)). You can built higher level constructs from a semaphore, like "event", "condition", "mutex" ("critical sections"). There are the older and newer interfaces in C. Some higher level languages like Python and Perl also expose the interface.

The "Mutex" that you are likely talking about is the pthread's mutex, which will be faster than anything in user space, especially one using a spinlock (which were designed for extremely low level OS level constructs). Some pthread's implementations may use an OS level semaphore or may use other constructs.

Under Linux, the POSIX mutex is implemented by a featured kernel facility named futex(2) which is a part of NPTL. It means that your thread traps into kernel-space ONLY when it needs suspending, or just a simple user-space function.

Spin-lock is so simple that needn't the kernel be involved at all, you can easily construct it just by something like __sync_bool_compare_and_swap(). Glibc implemented spin lock of cause -- man pthread_spin_init -- , but spin lock is not always "fast" as your imagination.