0

我读过函数式编程非常适合多线程程序,因为它带来了编程语言范式(不变性,无副作用的函数)。我还读到多线程程序通常是非确定性的。

鉴于stakx对类似(但不同)问题的回答,这是我的问题:

如果使用函数式编程语言进行编码,多线程程序可以是确定性的吗?

4

1 回答 1

0

Of course. Any program can be made deterministic. For example,

Thread.new do
  1 + 2
end
Thread.new do
  2 + 3
end

is deterministic, as it does not affect the universe in any fashion. It is all in the way you order your side-effects. If you have no mutable structures, it is then only the matter of ordering your input-output and interprocess communication predictably. But if you are asking if every functional program is deterministic, the answer is no: the moment your threads communicate or do IO, you need some explicit scheduling to make it work the same each time.

于 2014-12-24T03:48:25.987 回答