What is the difference between semaphore and mutex in implementation?

I read that the mutex and the binary semaphore differ in only one aspect, in the case of the mutex, the blocking stream must be unlocked, but in the semaphore, the blocking and unlocking stream can be different?

Which one is more effective?

+4
source share
1 answer

Assuming you know the main differences between sempahor and mutex:

For quick and easy synchronization, use the critical section.

To synchronize threads across process boundaries, use mutexes.

To synchronize access to limited resources, use a semaphore.

Besides the fact that mutexes have an owner, these two objects can be optimized for different uses. Mutexes are designed to hold only for a short time; Failure to do so may result in reduced productivity and unfair planning. For example, a running thread might be allowed to get a mutex, even if another thread is already locked on it, creating a deadlock . Semaphores can provide more justice, or justice can be invoked with the help of several conditional variables.

+4
source

Source: https://habr.com/ru/post/1339128/


All Articles