gpt4 book ai didi

linux - 完成变量和信号量之间的区别

转载 作者:IT王子 更新时间:2023-10-29 00:23:15 25 4
gpt4 key购买 nike

在 linux 内核中,信号量用于为数据的关键部分提供互斥,完成变量用于在等待事件的 2 个线程之间进行同步。为什么不使用信号量进行这样的同步呢?使用完成变量比使用信号量有什么优势吗?

最佳答案

为什么完成最初实现的解释: http://lkml.indiana.edu/hypermail/linux/kernel/0107.3/0674.html

The basic summary is that we had this (fairly common) way of waiting for certain events by having a locked semaphore on the stack of the waiter, and then having the waiter do a "down()" which caused it to block until the thing it was waiting for did an "up()".

This works fairly well, but it has a really small (and quite unlikely) race on SMP, that is not so much a race of the idea itself, as of the implementation of the semaphores. We could have fixed the semaphores, but there were a few reasons not to:

  • the semaphores are optimized (on purpose) for the non-contention case. The "wait for completion" usage has the opposite default case
  • the semaphores are quite involved and architecture-specific, exactly
    due to this optimization. Trying to change them is painful as hell.

So instead, I introduced the notion of "wait for completion":

关于完成与信号量的最新主题 http://lkml.org/lkml/2008/4/11/323

关于linux - 完成变量和信号量之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4764945/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com