Before encountering a problem, what is the difference between semaphores and mutexes? Forget to think for a moment, today just think of this problem, read and StackOverflow, understand and do simple finishing
first, the definition
mutexes, which are used to serialize access to a portion of reentrant code that cannot be executed concurrently by multiple threads
Semaphore, semaphore, limit the number of concurrent users for shared resources to the maximum number
Second, use
mutex: Suppose we have a key part of a thread T1 want to access it then it follows these steps:
- lock
- Use key section
- unlock
binary semaphore: It is based on signaling wait and signal work. Wait (s) to reduce the "s" value by one of the usual "s" values initialized with the value "1", the signal (s) increases the "s" Value by 1. A value of 0 indicates that the critical section is in use if the "s" value is 1 to indicate that no one is using the critical section. Assuming that the thread T2 is using a critical section, it follows these steps:
- Wait (s)//The initial S value is equal to its value minus 1 after the call, or 0
- Use key sections
- Signal (s)//Now increases in s value, becomes 1
Iii. Specific analysis (with insight)
A typical example of a mutex is a ticket: a ticket is a shared resource, and now there are two threads coming in to buy a ticket. If you do not lock the ticket with a mutex online thread, you may be "selling the same ticket to two different people (threads)". I don't think that's a lot to explain.
Ordinary people do not understand the difference between Semaphore and mutexes, the root cause is not know the use of semaphore. Semaphore's purpose, in a nutshell: scheduling Threads . Some people with Semaphore can also use the ticket "protection" in the above example to prevent the sharing of resource conflicts, it must be admitted that this is feasible, but semaphore not let you do this, if you want to do this, please use a mutex.
On the internet, including StackOverflow and other famous forums, there is a wide range of toilet examples: Mutex is a toilet a key, who grabbed the key who use the toilet, who did not grab on who will wait; Semaphore is more than the same toilet more than the same key----as long as you can get a key , you can look for an empty toilet to go in. In fact, this example is very bad for beginners, especially beginners who have just learned the mutex-----The first response I read to this example is: Semaphore is the thread pool??? So, Be sure to forget this example . Also, one would say that a mutex is the case where the value of semaphore is equal to 1. This sentence can not be said wrong, but for beginners, please take this sentence as a mistake, and so you will thoroughly integrate this part of the knowledge, you can really understand what this sentence means. In short, be sure to keep in mind thatthe work of the mutex and the semaphore do not mix up.
Here I simulate one of the most typical scenarios for using semaphore: a originates from a thread, b originates from another thread, and C = A + B is also a thread. (i.e. three threads total)
Obviously, the third thread must wait until the first to second thread finishes executing it. At this point, we need to
dispatch the thread : After the first to second thread has finished executing, the third thread is executed. At this point, you need to use the semaphore.
intA, B, C;voidGeta () {a=Calculatea (); Semaphore_increase ();}voidGetb () {b=Calculateb (); Semaphore_increase ();}voidgetc () {semaphore_decrease (); Semaphore_decrease (); C= A +b;} T1=thread_create (geta); T2=thread_create (GETB); T3=thread_create (GETC); Thread_join (T3);//The mechanism of semaphore I do not speak here, Baidu a bit you will know. //semaphore_increase Correspondence Sem_post//semaphore_decrease Correspondence Sem_wait
This is the most typical use of semaphore. To put it bluntly, the
dispatch thread is: some thread production (increase) while other threads consume (decrease), semaphore can keep production and consumption in a logical order of execution. the thread pool is completely different from the limit of the number of threads that a programmer has to start at the same time to avoid repeatedly creating and releasing threads, depending on the level of hardware and design requirements, and in order to achieve optimal performance. For example, if you want to calculate z = a + b +...+ x + y ... As a result, while each addend is a thread, the logical order between the compute Z thread and each addend thread is dispatched by semaphore, and the thread pool is used to allow up to a few threads at the same time as you run the program.
In short, a lock is a service to a shared resource, whereas a semaphore is a logical order of execution that serves multiple threads .
Please look back at the toilet example that we forgot. The reason why I let people forget this example is that if you learn semaphore from this point of view, you will certainly confuse the mutex. The essence of Semaphore is to dispatch the thread ----after fully understanding the concept, we look at this example.
Semaphore is a value for thread scheduling, so with this mechanism, we can also implement a limit on the number of threads. and when we limit the number of threads to 1 o'clock, you'll find that shared resources are protected------only one thread is running at any time, so shared resources are certainly equivalent to being protected. But I would like to remind you, if you want to protect the shared resources, use a mutex, whether you should use conditional locks or semaphore, be sure to think clearly. it is possible to protect shared resources through semaphore, but it is a mistake to semaphore.
As long as you can figure out why locks, condition locks, and semaphore are born, or what design needs they face, and what types of problems to solve, you will naturally not confuse them.
two back link: https://www.zhihu.com/question/47704079/answer/135859188 Source: Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.
The difference between a semaphore and an exclusive lock