Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In linux or a library for linux, is there an equivalent to critical section in win32?

I am aware of Mutex, but it is not the same as critical section since a critical section uses user-mode spinlock and an event object internally, and it is must be faster than mutex.

share|improve this question
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. – rozcietrzewiacz Jan 16 '12 at 10:08
"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. – Mat Jan 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 Bae Jan 17 '12 at 7:18
up vote 3 down vote accepted

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.

share|improve this answer

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.