我如何在Semaphore和SemaphoreSlim之间进行选择? [英] How do I choose between Semaphore and SemaphoreSlim?

查看:188
本文介绍了我如何在Semaphore和SemaphoreSlim之间进行选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它们的公共接口看起来很相似. 文档指出SemaphoreSlim是一种轻量级替代方案,并且不使用Windows内核信号量. 此资源指出SemaphoreSlim的速度要快得多.在什么情况下,SemaphoreSlim比Semaphore更有意义,反之亦然?

Their public interfaces appear similar. The documentation states that the SemaphoreSlim is a lightweight alternative and doesn't use Windows Kernel semaphores. This resource states that the SemaphoreSlim is much faster. In what situations does the SemaphoreSlim make more sense over the Semaphore and vice versa?

推荐答案

一个区别是SemaphoreSlim不允许命名信号量,该信号量可以在系统范围内使用.这意味着SemaphoreSlim不能用于跨进程同步.

One difference is that SemaphoreSlim does not permit named semaphores, which can be system-wide. This would mean that a SemaphoreSlim could not be used for cross-process synchronization.

MSDN文档还指出,当等待时间预计将非常短".通常,在大多数情况下,苗条版本的重量更轻,这一点通常可以很好地与之吻合.

The MSDN documentation also indicates that SemSlim should be used when "wait times are expected to be very short". That would usually dovetail nicely with the idea that the slim version is more lightweight for most of the trade offs.

这篇关于我如何在Semaphore和SemaphoreSlim之间进行选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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