[英]When/why use an MVar over a TVar
I find TVar's quite easy to work with even though MVar's appear a little simpler, while TVar's a little more featureful. 我觉得TVar很容易使用,即使MVar看起来更简单一些,而TVar更有特色。
So my question is pretty simple, what condition do I want to go to MVar rather than TVar? 所以我的问题很简单,我想要什么条件去MVar而不是TVar? I suppose anytime I don't need transactional update I can use an MVar, but in what way does that benefit me?
我想任何时候我都不需要事务更新我可以使用一个MVar,但这对我有什么好处?
MVar 无功
TVar
in some cases TVar
更快 TVar 的TVar
IORef IOREF
IO
writes/updates IO
写入/更新 TVar
TVar
There is not really a hard and fast rule for when to use MVar
or TVar
. 什么时候使用
MVar
或TVar
并没有真正的硬性规则。 If the resource I'm guarding will ever be "missing" (as opposed to empty, consider Nothing
vs Just mempty
), then MVar
often makes the most sense. 如果我正在守卫的资源将“失踪”(而不是空,请考虑
Nothing
vs Just mempty
),那么MVar
通常是最有意义的。 If I will need to perform atomic blocks of modifications to the resource, then TVar
is most suitable. 如果我需要对资源执行原子块修改,那么
TVar
是最合适的。
TVars are safer but slower. 电视更安全但速度更慢。
MVars can deadlock, but are much, much more efficient. MVars可以死锁,但效率更高。
More efficient still is IORef
and atomicModifyIORef
(CAS), but that's highly restricted in what you can do with it. 更高效的仍然是
IORef
和atomicModifyIORef
(CAS),但是你可以用它来做很多限制。
It's really a safety over performance trade off. 这实际上是对性能权衡的安全性。 TVars are fully general, very safe.
TVars非常通用,非常安全。 Everything else is less so, on a decreasing scale.
其他一切都不那么严重,规模越来越小。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.