简体   繁体   中英

Waiting on mutex vs waiting on semaphore in terms of CPU-time

I wonder if there's any difference between waiting on a locked mutex and waiting on a locked semaphore, in terms of CPU-usage. According to the man pages, at both scenarios the thread blocks, meaning it is transferred to a waiting queue, thus not consuming CPU-time.

On the contrary, I did see a few places and answers that claim that waiting on a mutex causes busy-waiting, which means consuming CPU-time to no avail.

I'd appreciate if you could explain whether there's a difference or not.

Edit: My question was identified as a possible duplicate of this question , however, like I noted, I see opposite answers in some other places in this forum, that claim that waiting on a mutex is a busy-waiting.

Surely this depends on the OS and compiler implementation that you have used.

However, since a mutex can be implemented as a version of a semaphore, then the worst case would be that the mutex performs as a semaphore. There would be no reason to code it less efficently.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM