Mutexes and critical sections

Wednesday 16 April 2003This is 20 years old. Be careful.

I’m annoyed by the fact that Win32 has two separate synchronization primitives, one called a mutex, and another called a critical section. I’m annoyed because these two work exactly the same, except one can be used across processes, and is therefore more heavyweight, than the other. Except for that, they are the same.

So why were they given those names? These are both mutexes. The name “critical section” doesn’t even make any sense for an allocated object that can be locked and unlocked. What is it a section of, and in what sense is it critical?

The name “critical section” comes from the concurrent programming idea of a chunk of code that cannot be entered simultaneously by more than one thread. It is a critical section of code. Of course, a mutex is a good way to protect a critical section, and critical sections need not be protected across processes, so you can see how the term came to be misused for a lightweight mutex.

The Win32 definition of the term is being presented as authoritative in some glossaries, which is unfortunate. If you’ve learned about thread programming on Win32, try to remember: Microsoft’s names for these things are a little off.


Naming is hard and its even harder to rename.

In retrospect, Microsoft should have called them something like "process" and "global" mutexs to make the distinction. Sun makes this sort of distinction with threads on Solaris -- there are "user" and "kernel" threads. The former is only for programmer convenience and not pre-emptive and can't be independently scheduled on different processors. Microsoft has made a similar weird name choice here: they have "threads" and "fibers". The latter are like "user threads" in Solaris.

No question, this is confusing. Especially because most people call such objects "semaphores", and Windows has an object called a semaphore, but it doesn't have the expected semantics. The first time I heard "Mutex" I thought it was a joke. (Yeah, mutually exclusive, I get it, but the name still sucks.) "Critical Section" is more descriptive but this is an unexpected usage.


Add a comment:

Ignore this:
Leave this empty:
Name is required. Either email or web are required. Email won't be displayed and I won't spam you. Your web site won't be indexed by search engines.
Don't put anything here:
Leave this empty:
Comment text is Markdown.