Can a multithreaded functional program be deterministic?

308 Views Asked by At

I have read that functional programming is pretty well suited for multithreaded programs given the programming language paradigms it brings (immutability, side effect-free functions). I have also read that multithreaded programs are often non nondeterministic.

Given stakx answer to a similar (but different) question, here is my question:

Can a multithreaded program be deterministic if coded using functional programming languages?

1

There are 1 best solutions below

2
On BEST ANSWER

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.