Semaphore and mutex, which is faster?

If you are considering binary semaphore and mutex, which one is faster? I mean, it takes fewer instructions. What additional data does the mutex support compared to the semaphore?

+3
source share
1 answer

It depends on the implementation, but you will probably find that the mutex is implemented a little faster. Mutexes are usually implemented using a test and a set, while semaphores are often implemented using a test and an increment, or as a mutex protecting a variable that increases.

I would suggest using a mutex in most cases, but not because of speed; simply because code written using mutexes is easier to understand because semantics are less complex.

+8
source

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


All Articles