一种形成“选择”的方法无需轮询即可查看 MVar
我有两个 MVar(一个 MVar 和一个 Chan)。我需要从 Chan 中取出东西并处理它们,直到另一个 MVar 不再为空。我理想的解决方案类似于 UNIX select
函数,其中我传入一个(可能是空的)MVar 列表,并且线程会阻塞,直到其中一个已满,然后返回完整的 MVar。尽我所能,除了用 isEmptyMVar 反复轮询每个 MVar 直到出现错误为止,我想不出有什么办法可以做到这一点。这看起来效率很低。
另一个想法是使用 throwTo,但它会中断线程中正在发生的事情,我需要以原子方式完成 Chan 中的作业处理。
我打字时的最后一个想法是为每个 MVar 创建一个新的 forkIO,它尝试读取其 MVar,然后用它自己的实例填充新创建的 MVar。然后,原始线程可能会在该 MVar 上阻塞。 Haskell 线程是否足够便宜来运行那么多线程?
I have two MVars (well an MVar and a Chan). I need to pull things out of the Chan and process them until the other MVar is not empty any more. My ideal solution would be something like the UNIX select
function where I pass in a list of (presumably empty) MVars and the thread blocks until one of them is full, then it returns the full MVar. Try as I might I can think of no way of doing this beyond repeatedly polling each MVar with isEmptyMVar until I get false. This seems inefficient.
A different thought was to use throwTo, but it interrupts what ever is happening in the thread and I need to complete processing a job out the the Chan in an atomic fashion.
A final thought as I'm typing is to create a new forkIO for each MVar which tries to read its MVar then fill a newly created MVar with its own instance. The original thread can then block on that MVar. Are Haskell threads cheap enough to go running that many?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Haskell 线程非常便宜,因此您可以通过这种方式解决它,但听起来 STM 更适合您的问题。使用 STM,您可以执行以下操作:
由于
retry
和orElse
的行为,此代码尝试获取a
,如果失败,则获取b
。如果两者都失败,它将阻塞,直到其中任何一个更新并重试。您甚至可以使用它来制作您自己的
select
的基本版本:Haskell threads are very cheap, so you could solve it that way, but it sounds like STM would be a better fit for your problem. With STM you can do
Because of the behavior of
retry
andorElse
, this code tries to geta
, then if that fails, getb
. If both fail, it blocks until either of them is updated and tries again.You could even use this to make your own rudimentary version of
select
:如何使用 STM 版本、
TChan
和TVar
以及retry
和orElse
行为?实现 select 是 STM 的一项出色功能。来自“可组合内存事务”:
orElse
在 RWH 中。How about using STM versions,
TChan
andTVar
, with theretry
andorElse
behavior?Implementing select is one of STM's nice capabilities. From "Composable Memory Transactions":
orElse
in RWH.