The difference between Linux mutex and semaphore and applicable scenarios

The difference between mutex and semaphore

1. Mutex is used for mutual exclusion of threads :

  • Mutual exclusion: locking and unlocking means that only one visitor is allowed to access a certain resource at the same time (such as atomic operation of a piece of code or a variable) , which is unique and exclusive. But mutual exclusion cannot limit the order in which visitors access resources (this needs to be solved by synchronization), that is, access is out of order, and after the lock is released, whoever can compete will access the resource.

2. The semaphore is used for thread synchronization :

  • Synchronization: P–, V++ operation, refers to the orderly access of visitors to resources through other mechanisms (such as atomic operation of a counter) on the basis of mutual exclusion .

In most cases, synchronization already implements mutual exclusion (spin lock), and in particular all writes to resources must be mutually exclusive. In rare cases, multiple visitors can be allowed to access resources at the same time.

Applicable scenario 1 (sequential angle)

The mutex value can only be 0/1 , and the semaphore value can be a non-negative integer (when the initial value of the semaphore is set to 1, the semaphore is actually equivalent to a mutex lock)

  • Mutexes can be used in scenarios where there is no sequence and no synchronization requirements between threads. It is often used to protect the correct access to adjacent resources; such as grabbing tickets, the order is random , as long as the number of shared tickets is correctly reduced;

  • Semaphores can be used in the production line . For example, we have ABC products, and B can only be produced after A is produced, and C can only be produced after B is produced. We can set three ABC semaphores to represent the number of their remaining products , so that each thread During production, only P() is needed--the semaphore of the previous product you need >=0 can be produced, otherwise it will enter the blocking waiting queue and wait for the V operation to wake up; if one is successfully produced, V()++ can reach The purpose of synchronizing other threads ensures a certain order of synchronization; it is more convenient than repeatedly locking and unlocking;

Applicable scenario 2 (underlying principle analysis)

  • Mutex is locked and unlocked, which is generally used in the kernel to protect a piece of code; switching into the kernel state, etc., if the lock time is not long, the overhead is too large, suitable for scenarios where the waiting time is short;
  • The sem semaphore is generally used in the kernel to protect a variable; the bottom layer is a spin lock, which is more efficient than a mutex if the waiting time is short, but if the waiting time is long, it will occupy the cpu and spin all the time, which is not worth the candle;

The atomic atomic operation of C++11 (the system bus principle of locking CPU access), is actually similar to the mutex usage of linux, and can achieve thread safety for accessing critical resources, but the efficiency is different. One is locked into the kernel and the other is accessed by the CPU. Lock on the bus;
at the same time:
isn’t the atomic type of bool a mutex~
The atomic type of unsigned int is a sem semaphore~

おすすめ

転載: blog.csdn.net/wtl666_6/article/details/129699100