Mutex
lastmod 2025-08-22created 2020-09-117 edits
(The Lock (computer science)/Lock/Locking pages are moved to this page)
In computer science, a lock or mutex (from mutual exclusion) is a synchronization mechanism for enforcing limits on access to a resource in an environment where there are many threads of execution. It is also called locking. A lock is designed to enforce a mutual exclusion concurrency control policy.
Types
Generally, locks are advisory locks, where each thread cooperates by acquiring the lock before accessing the corresponding data. Some systems also implement mandatory locks, where attempting unauthorized access to a locked resource forces an exception.
The simplest type of lock is a binary semaphore. It provides exclusive access to the locked data. Other schemes also provide shared access for reading data. Other widely implemented access modes are exclusive, intend-to-exclude, and intend-to-upgrade.
Comparison of Mutex functions in Windows and Linux
The difference between a Mutex and a Windows Critical Section is whether it is a User-mode object or a Kernel object.
Because a Critical Section is a User-mode object, it is used to set up critical sections between threads within a single process, whereas a Mutex is a kernel-mode object and can be used to set up critical sections across multiple processes.
In Linux, there is no difference between processes and threads, so it is implemented only with Mutex.
| Function | Windows | Linux |
| Create | CreateMutex | pthread_mutex_init |
| Destroy | CloseHandle | pthread_mutex_destroy |
| Enter | WaitForSingleObject | pthread_mutex_lock |
| Exit | ReleaseMutex | pthread_mutex_unlock |
See also
- Critical section
- flock
- Semaphore
- pthread
- Critical section
- File locking
- Monitor (synchronization)
- Korean: 세마포어
- Blocking
- Web Locks API
- Fast Userspace Mutex (futex)