为什么并发 haskell 是非确定性的,而并行 haskell 原语(par 和 pseq)是确定性的?

发布于 2024-12-22 08:31:23 字数 49 浏览 2 评论 0原文

不太了解 Haskell 中并发性和并行性背景下的决定论。一些例子会有所帮助。 谢谢

Don't quite understand determinism in the context of concurrency and parallelism in Haskell. Some examples would be helpful.
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暮凉 2024-12-29 08:31:23

当处理纯值时,求值的顺序并不重要。这本质上就是并行性的作用:并行评估纯值。与纯值相反,顺序通常对于具有副作用的操作很重要。同时运行操作称为并发

例如,考虑两个操作 putStr "foo"putStr "bar"。根据评估这两个操作的顺序,输出是“foobar”、“barfoo”或两者之间的任何状态。输出不确定,因为它取决于特定的评估顺序。

作为另一个示例,请考虑两个值 sum [1..10]5 * 3。无论这两者的评估顺序如何,它们总是会减少到相同的结果。这种决定论通常只能用纯值来保证。

When dealing with pure values, the order of evaluation does not matter. That is essentially what parallelism does: Evaluating pure values in parallel. As opposed to pure values, order usually matters for actions with side-effects. Running actions simultaneously is called concurrency.

As an example, consider the two actions putStr "foo" and putStr "bar". Depending on the order in which those two actions get evaluated, the output is either "foobar", "barfoo" or any state in between. The output is indeterministic as it depends on the specific order of evaluation.

As another example, consider the two values sum [1..10] and 5 * 3. Regardless of the order in which those two get evaluated, they always reduce to the same results. This determinism is something you can usually only guarantee with pure values.

羁拥 2024-12-29 08:31:23

并发和并行是两个不同的东西。

并发意味着多个线程不确定地交互。例如,您可能有一台聊天服务器,其中每个客户端都由一个线程处理。非确定性对于您尝试建模的系统至关重要。

并行性是指使用多个线程来简单地使程序运行得更快。但是,最终结果应该与顺序运行算法完全相同。

许多语言没有并行原语,因此您必须使用线程和锁等并发原语来实现它。但是,这意味着程序员必须小心,确保不会意外引入不需要的非确定性或其他并发问题。有了像 par 和 pseq 这样的显式并行原语,许多问题就迎刃而解了。

Concurrency and parallelism are two different things.

Concurrency means that you have multiple threads interacting non-deterministically. For example, you might have a chat server where each client is handled by one thread. The non-determinism is essential to the system you're trying to model.

Parallelism is about using multiple threads for simply making your program run faster. However, the end result should be exactly the same as if you run the algorithm sequentially.

Many languages don't have primitives for parallelism, so you have to implement it using concurrency primitives like threads and locks. However, this means that you the programmer have to be careful to ensure that you don't accidentally introduce unwanted non-determinism or other concurrency issues. With explicit parallelism primitives like par and pseq, many of these concerns simply go away.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文