简体   繁体   English

在等待互斥锁与等待信号量方面的CPU时间

[英]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. 我想知道在等待CPU占用率方面,等待锁定的互斥锁和等待信号灯之间是否有区别? According to the man pages, at both scenarios the thread blocks, meaning it is transferred to a waiting queue, thus not consuming CPU-time. 根据手册页,在这两种情况下,线程都会阻塞,这意味着将其转移到等待队列中,因此不会占用CPU时间。

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. 相反,我确实看到了一些地方和答案,它们声称等待互斥锁会导致等待繁忙,这意味着浪费CPU时间无济于事。

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. 当然,这取决于您使用的OS和编译器实现。

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. 没有理由对它进行低效率的编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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