Difference: 1. Mutex is used for mutual exclusion of threads, and semaphore is used for thread synchronization; 2. Mutex value can only be 0 or 1, and semaphore value can be a non-negative integer; 3. The locking and unlocking of the mutex must be used by the same thread respectively. The semaphore can be released by one thread and obtained by another thread.
The operating environment of this tutorial: Windows 7 system, Dell G3 computer.
The difference between mutex and semaphore
1. Mutex is used for mutual exclusion of threads, and semaphore is used for synchronization of threads.
This is the fundamental difference between mutexes and semaphores, that is, the difference between mutual exclusion and synchronization.
Mutual exclusion: refers to a resource that only allows one visitor to access it at the same time, and is unique and exclusive. But mutual exclusion cannot limit the order in which visitors access resources, that is, access is unordered.
Synchronization: refers to the orderly access of resources by visitors through other mechanisms on the basis of mutual exclusion (in most cases). In most cases, synchronization already implements mutual exclusion, especially when all writes to resources must be mutually exclusive. A few cases allow multiple visitors to access resources at the same time
2. The mutex value can only be 0/1, and the semaphore value can be a non-negative integer.
In other words, a mutex can only be used for mutually exclusive access to one resource, and it cannot implement multi-thread mutual exclusion of multiple resources. Semaphore can realize multi-thread mutual exclusion and synchronization of multiple similar resources. When the semaphore is a single-valued semaphore, mutually exclusive access to a resource can also be completed.
3. The locking and unlocking of the mutex must be used by the same thread respectively. The semaphore can be released by one thread and obtained by another thread.
Mutex (Mutex)
The mutex is a data structure that represents the mutual exclusion phenomenon and is also used as a binary semaphore. A mutex is basically a multitasking-sensitive binary signal that can be used to synchronize the behavior of multiple tasks. It is often used to protect critical sections of code from interrupts and to share resources used in synchronization.
Mutex is essentially a lock, providing exclusive access to resources, so the main function of Mutex is for mutual exclusion. The value of the Mutex object has only two values 0 and 1. These two values also represent the two states of Mutex respectively. The value is 0, indicating the locking state. The current object is locked. If the user process/thread attempts to Lock critical resources, it will enter the queue and wait; the value is 1, indicating the idle state. The current object is idle, and the user process/thread can Lock critical resources. Afterwards, the Mutex value decreases by 1 and becomes 0.
Mutex can be abstracted into four operations:
-Create
-Lock
-Unlock
- DestroyDestroy
Mutex can have an initial value when it is created, indicating whether the Mutex is in a locked state or an idle state after it is created. In the same thread, in order to prevent deadlock, the system does not allow the Mutex to be locked twice in a row (the system usually returns immediately after the second call). In other words, the two corresponding operations of locking and unlocking need to be completed in the same thread.
Mutex functions provided in different operating systems:
Action/System |
Win32 |
Linyx |
Solaris |
Create | CreateMutex |
##pthread_mutex_init | mutex_init |
Lock | WaitForSingleObject | pthread_mutex_lock | mutex_lock |
Unlock | ReleaseMutex | pthread_mutex_unlock | mutex_unlock |
Destroy | CloseHandle | pthread_mutex_destroy | mutex_destroy |
Action/System |
Win32 |
POSIX |
Create |
CreateSemaphore |
##sem_init |
Wait | WaitForSingleObject | sem _wait |
Release | ReleaseMutex | sem _post |
Try to wait | WaitForSingleObject | sem _trywait |
Destroy | CloseHandle | sem_destroy |
The above is the detailed content of What is the difference between semaphore and mutex. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools