完成变量和信号量之间的区别 [英] Difference between completion variables and semaphores

查看:266
本文介绍了完成变量和信号量之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In the linux kernel, semaphores are used to provide mutual exclusion for critical sections of data and Completion variables are used to synchronize between 2 threads waiting on an event. Why not use semaphores for such a synchronization ? Is there any advantage of using a completion variable over a semaphore ?

推荐答案

您可能要使用补全而不是信号量有两个原因.首先,多个线程可以等待完成,并且可以通过一次调用complete_all()将它们全部释放.使信号量唤醒未知数量的线程更为复杂.

There are two reasons you might want to use a completion instead of a semaphore. First, multiple threads can wait for a completion, and they can all be released with one call to complete_all(). It's more complex to have a semaphore wake up an unknown number of threads.

第二,如果等待线程要取消分配同步对象,则在使用信号量时会出现争用情况.也就是说,在使用up()完成唤醒线程之前,服务员可能会被唤醒并释放对象.此竞赛不存在用于完成的竞赛. (请参阅Lasse的帖子.)

Second, if the waiting thread is going to deallocate the synchronization object, there is a race condition if you're using semaphores. That is, the waiter might get woken up and deallocate the object before the waking thread is done with up(). This race doesn't exist for completions. (See Lasse's post.)

这篇关于完成变量和信号量之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆