What is the difference between semaphore and mutex
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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics



8-core means that the CPU has 8 physical cores, and 16-thread means that the CPU can have up to 16 threads processing tasks at the same time. The number of cores and threads are important performance indicators of a computer CPU. The higher the number of cores of the CPU, the higher the processing speed; the higher the number of threads, the more conducive it is to running multiple programs at the same time, because the number of threads is equivalent to the number of times the CPU can run at the same time at a certain moment. The number of tasks to be processed in parallel. Multi-threading can maximize wide-issue, out-of-order superscalar processing, improve the utilization of processor computing components, and alleviate memory access delays caused by data correlation or cache misses.

With the advancement of the times and the continuous updating of technology, the demand for Web applications is increasing, and PHP programs have become one of the main programming languages for many Web applications. In a multi-threaded web application, concurrency and race conditions must be taken into account to ensure that the program runs correctly. In PHP programs, mutexes provide a solution to ensure thread safety and accuracy of data transfer. In this article, we will explore mutex best practices in PHP programs. What is a mutex? A mutex is used to ensure that threads

To avoid thread starvation, you can use fair locks to ensure fair allocation of resources, or set thread priorities. To solve priority inversion, you can use priority inheritance, which temporarily increases the priority of the thread holding the resource; or use lock promotion, which increases the priority of the thread that needs the resource.

Thread termination and cancellation mechanisms in C++ include: Thread termination: std::thread::join() blocks the current thread until the target thread completes execution; std::thread::detach() detaches the target thread from thread management. Thread cancellation: std::thread::request_termination() requests the target thread to terminate execution; std::thread::get_id() obtains the target thread ID and can be used with std::terminate() to immediately terminate the target thread. In actual combat, request_termination() allows the thread to decide the timing of termination, and join() ensures that on the main line

"Thread" is the smallest unit of instruction flow when a program is running. A process refers to a program with certain independent functions, and a thread is a part of the process, describing the execution status of the instruction flow; the thread is the smallest unit of the instruction execution flow in the process, and is the basic unit of CPU scheduling. A thread is an execution process of a task (a program segment); a thread does not occupy memory space, it is included in the memory space of the process. Within the same process, multiple threads share the process's resources; a process has at least one thread.

Differences: 1. A thread can have multiple coroutines, and a process can also have multiple coroutines alone; 2. Threads are a synchronization mechanism, while coroutines are asynchronous; 3. Coroutines can retain the state of the last call, Threads do not work; 4. Threads are preemptive, while coroutines are non-preemptive; 5. Threads are divided CPU resources, and coroutines are organized code processes. Coroutines require threads to host and run.

During the development of JavaFX applications, we often encounter JavaFX thread stuck errors. Such errors vary in severity and may adversely affect program stability and performance. In order to ensure the normal operation of the program, we need to understand the causes and solutions of JavaFX thread stuck errors, and how to prevent this error from occurring. 1. The cause of JavaFX thread stuck error. JavaFX is a multi-threaded UI application framework, which allows programs to execute for a long time in background threads.

Processes and threads in Go language: Process: an independently running program instance with its own resources and address space. Thread: An execution unit within a process that shares process resources and address space. Features: Process: high overhead, good isolation, independent scheduling. Threads: low overhead, shared resources, internal scheduling. Practical case: Process: Isolating long-running tasks. Threads: Process large amounts of data concurrently.