Similarities and differences between mutex/semphore/binary semphore

Source: Internet
Author: User

 

Mutex is a key. When a person takes the key, they can enter a room. When they come out, the key is handed over to the first in the queue. Generally, this method is used to serialize the access to the critical section code to ensure that the Code is not run in parallel.

 

Semaphore is a room that can accommodate n people. If not, you can enter the room. If the person is full, you have to wait for someone to come out. For n = 1, it is called binary semaphore. It is generally used to restrict simultaneous access to a resource.

 

Differences between binary semaphore and mutex:

In some systems, binary semaphore and mutex are not different. In some systems, the main difference is that mutex must be released by the process that acquires the lock. The semaphore can be released by other processes (in this case, semaphore is actually an atomic variable, which can be added or subtracted). Therefore, semaphore can be used for inter-process synchronization. The synchronization function of semaphore is supported by all systems, and whether mutex can be released by other processes is not fixed. Therefore, we recommend that mutex only protect the critical section. Semaphore is used to protect a variable or synchronize data.

 

Another concept is spin lock, which is a kernel state concept. The main difference between spin lock and semaphore is that spin lock is busy waiting, while semaphore is sleep. Busy waiting is meaningless for sleep processes. For a single CPU system, busy waiting is of course meaningless (no CPU can release the lock ). Therefore, spin lock is used only for multi-CPU kernel-State non-process space. In the case of non-SMP, the Linux kernel spin lock only shuts down IRQ and has no other operations. It is used to ensure that the running of this program will not be interrupted. In fact, it is similar to mutex, which serializes the access to the critical section. However, mutex cannot protect the interruption or be called in the interrupt processing program. The spin lock is generally not necessary for sleep process space.

 

From above: http://www.yuanma.org/data/2008/0612/article_3076.htm

 

About the difference between semaphore and mutex, There is a famous toilet theory on the Internet (http://koti.mbnet.fi/niclasw/MutexSemaphore.html ):

Mutex: is a key to a toilet. one person can have the key-occupy the toilet-at the time. when finished, the person gives (frees) the key to the next person in the queue. officially: "mutexes are typically used to serialise access to a section of re-entrant code that cannot be executed concurrently by more than one thread. A mutex object only allows one thread into a controlled section, forcing other threads which attempt to gain access to that section to wait until the first thread has exited from that section."
Ref: Symbian Developer Library (A mutex is really a semaphore with value 1 .)

Semaphore:

Is the number of free identical toilet keys. example, say we have four toilets with identical locks and keys. the semaphore count-the Count of keys-is set to 4 at beginning (all four toilets are free), then the Count value is decremented as people are coming in. if all toilets are full, ie. there are no free keys left, the semaphore count is 0. now, when EQ. one person leaves the toilet, semaphore is increased to 1 (one free key), and given to the next person in the queue.

Officially: "A semaphore restricts the number of simultaneous users of a shared resource up to a maximum number. threads can request access to the resource (decrementing the semaphore), and can signal that they have finished using the resource (incrementing the semaphore )."
Ref: Symbian Developer Library

Therefore, mutex is a binary semaphore (the value is 0 or 1 ). But what are their differences? There are two main aspects:

* The initial state of mutex is different: the initial value of mutex is 1 (indicating the lock available), and the initial value of semaphore is 0 (indicating the status of unsignaled ). The subsequent operations are basically the same. Mutex_lock and sem_post both change the value from 0 to 1, mutex_unlock and sem_wait both change the value from 1 to 0 (if the value is zero, wait ). The initial value determines: Although both mutex_lock and sem_wait perform the V operation, sem_wait immediately blocks the current thread until other threads post. mutex_lock can be entered in the initial state.
* Usage is different (symmetric vs. Asymmetric): Here we talk about "usage ". Semaphore implements signal, but mutex also has signal (when one thread is locked and another thread is unlocked, the lock thread will receive this signal and continue to run ). In the use of mutex, the model is symmetric. The unlock thread must also be locked first. Semaphore is a non-symmetric model. For a semaphore, only one side is post and the other side is wait. Taking the above toilet theory as an example, mutex is a key that is repeatedly used and transmitted between various threads, and semaphore is a key continuously created by one party, for use by the other party (the other party does not need to return the data ).

The previous experiments show that mutex can indeed implement post and wait functions, but it is not needed because it is "mutex" rather than semaphore.

From above: http://hi.baidu.com/d_life/blog/item/c52d7501a96629de267fb54c.html

 

Mutex and semphore official explanations:

 

Mutex:

 

Is a key to a toilet. one person can have the key-occupy the toilet-at the time. when finished, the person gives (frees) the key to the next person in the queue.

 

Officially: "mutexes are typically used to serialise access to a section of re-entrant code that cannot be executed concurrently by more than one thread. A mutex object only allows one thread into a controlled section, forcing other threads which attempt to gain access to that section to wait until the first thread has exited from that section. "Ref: Symbian Developer Library (A mutex is really a semaphore with value 1 .)

 

Semaphore:

 

Is the number of free identical toilet keys. example, say we have four toilets with identical locks and keys. the semaphore count-the Count of keys-is set to 4 at beginning (all four toilets are free), then the Count value is decremented as people are coming in. if all toilets are full, ie. there are no free keys left, the semaphore count is 0. now, when EQ. one person leaves the toilet, semaphore is increased to 1 (one free key), and given to the next person in the queue.

 

Officially: "A semaphore restricts the number of simultaneous users of a shared resource up to a maximum number. threads can request access to the resource (decrementing the semaphore), and can signal that they have finished using the resource (incrementing the semaphore ). "Ref: Symbian Developer Library

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.