The semaphore in the process is the same as the mutex in the thread (mutex lock)

Semaphore and Mutex 
The difference between semaphore and ordinary integer variable:

① A semaphore is a non-negative integer variable. In addition to initialization, it can only be accessed through two standard atomic operations: wait(semap) , signal(semap) ;

②Operations are also called PV primitives (P comes from Dutch proberen "test", V comes from Dutch verhogen "increase"), and ordinary integer variables can be accessed in any statement block;

Difference between semaphore and mutex:

  1. Mutexes are used for mutual exclusion of threads, and signal lines are used for thread synchronization.

This is the fundamental difference between a mutex and a semaphore, which is the difference between a mutex and a synchronization.

Mutual exclusion: It means that only one visitor is allowed to access a resource at the same time, which is unique and exclusive. But mutual exclusion cannot limit the order in which visitors access resources, that is, the access is unordered.

Synchronization: On the basis of mutual exclusion (in most cases), the orderly access of visitors to resources is achieved through other mechanisms. In most cases synchronization already implements mutual exclusion, in particular all writes to resources must be mutually exclusive. A few cases where multiple visitors can be allowed to access the resource at the same time

  1. Mutex values ​​can only be 0/1, and semaphore values ​​can be non-negative integers.

That is to say, a mutex can only be used for the mutual exclusive access of one resource, and it cannot realize the multi-thread mutual exclusion of multiple resources. Semaphore can realize multi-thread mutual exclusion and synchronization of multiple resources of the same type. When the semaphore is a single-valued semaphore, a mutually exclusive access to a resource can also be accomplished.

  1. The locking and unlocking of the mutex must be used by the same thread respectively, and the semaphore can be released by one thread and obtained by another thread.

signal

A semaphore, sometimes called a semaphore, is a facility used in a multithreaded environment, which is responsible for coordinating individual threads to ensure that they can use common resources correctly and reasonably.

The semaphore can be divided into several categories: 
² Binary semaphore: Only the semaphore is allowed to take the value of 0 or 1, and it can only be acquired by one thread at the same time.

² Integer semaphore: The value of the semaphore is an integer, which can be acquired by multiple threads at the same time until the value of the semaphore becomes 0.

² Record semaphore: In addition to an integer value (count), each semaphore s has a waiting queue List, which is the identifier of each thread blocked on the semaphore. When a semaphore is released and the value is incremented by one, the system automatically wakes up a waiting thread from the waiting queue to obtain the semaphore, and at the same time, the semaphore is decremented by one.

A semaphore controls access to a shared resource through a counter. The value of the semaphore is a non-negative integer that is decremented by one for all threads passing through it. If the counter is greater than 0, the access is allowed and the counter is decremented by 1; if it is 0, the access is forbidden and all threads trying to pass it will be in a wait state.

The result of the counter calculation is a passport that allows access to the shared resource. So, in order to access the shared resource, the thread must get a pass from the semaphore, if the count of the semaphore is greater than 0, this thread gets a pass, which will cause the count of the semaphore to be decremented, otherwise, this thread will block until a pass is obtained until. When this thread no longer needs access to the shared resource, it releases the pass, which causes the semaphore's count to increment, and if another thread is waiting for the pass, that thread will get the pass at that time.

Semaphore can be abstracted into five operations: 
- Create Create

  • Wait Wait:

The thread waits for the semaphore. If the value is greater than 0, it is obtained, and the value is reduced by one; if it is only equal to 0, the thread goes to sleep until the semaphore value is greater than 0 or times out.

- release Post

When the semaphore is released, the value is incremented by one; if there is a waiting thread at this time, the thread is woken up.

- Tried to wait for TryWait

If TryWait is called, the thread does not actually acquire the semaphore, but checks whether the semaphore can be acquired. If the semaphore value is greater than 0, TryWait returns success; otherwise, it returns failure.

- Destroy Destroy

A semaphore can be used to protect two or more critical code segments that cannot be called concurrently. Before entering a critical section of code, a thread must acquire a semaphore. If there are no threads in the critical code segment, the threads immediately enter that part of the block diagram. Once the critical code segment completes, the thread must release the semaphore. Other threads that want to enter the critical section must wait until the first thread releases the semaphore. In order to complete this process, you need to create a semaphore, and then place the Acquire Semaphore VI and Release Semaphore VI at the beginning and end of each key code segment. Verify that these Semaphore VIs refer to the semaphore that was originally created. Action\System 
Win32 
POSIX

CreateCreateSemaphore  sem_init

wait 
WaitForSingleObject 
sem _wait

Release 
ReleaseMutex 
sem _post

Trying to wait 
WaitForSingleObject 
sem _trywait

DestroyCloseHandle 
sem_destroy 
_

Mutex

A mutex is a data structure that expresses the phenomenon of mutual exclusion, 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 multitasking, it is often used to protect critical sections of code from interrupts and to share resources used for synchronization. 
Mutex is essentially a lock that provides 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 the Mutex. If the value is 0, it means the locked state, the current object is locked, if the user process/thread tries to lock the critical resource, it will enter the queue to wait; if the value is 1, it means the idle state, the current object is idle, the user process/thread can lock the critical resource, After that, the Mutex value is reduced by 1 to 0.

Mutex can be abstracted into four operations:

  • CreateCreate

  • lock

  • UnlockUnlock

  • Destroy Destroy

When a Mutex is created, it can have an initial value, 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 generally returns immediately after the second call). That is to say, the two corresponding operations of locking and unlocking need to be completed in the same thread.

Mutex functions available in different operating systems: Action\System 
Win32 
Linyx 
Solaris

创建 
CreateMutex 
pthread_mutex_init 
mutex_init

加锁 
WaitForSingleObject 
pthread_mutex_lock 
mutex_lock

解锁 
ReleaseMutex 
pthread_mutex_unlock 
mutex_unlock

销毁 
CloseHandle 
pthread_mutex_destroy 
mutex_destroy

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324358163&siteId=291194637