Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Semaphore and mutex, which one is faster?

If you consider a binary semaphore and mutex, which one is faster? I mean, takes less instructions. What additional data does mutex maintain compared to semaphore?

like image 901
Boolean Avatar asked Nov 19 '25 00:11

Boolean


1 Answers

This is implementation dependent, but you will probably find that mutex has been implemented to be slightly faster. Mutexes are typically implemented with test and set, while semaphores are often implemented with test and increment or as a mutex guarding a variable that is incremented.

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

like image 180
Michael Aaron Safyan Avatar answered Nov 20 '25 18:11

Michael Aaron Safyan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!