Mon Mar 24 13:10:00 CET 2003
On Mon, 24 Mar 2003, Joe Armstrong wrote:
> Indeed it is :-) - I often wonder why we made spawn/3 a primitive and
> not just spawn a "universal" empty process and then send it a message
> telling it what to do.
> This is particularly useful when you need to setup sets of
> mutually recursive processes, then you can say:
> Pid1 = spawn()
> Pid2 = spawn(),
> Pid1 ! fun() -> ... Pid2 ... end,
> Pid2 ! fun() -> ... Pid1 ... end,
However, when you set up processes like that, things can easily go
wrong. If a process (Pid2 above) first of all enters a state where it
expects to be told what to do next, and another process (Pid1) thinks
that it's OK to talk to Pid2, then if the scheduler plays a little trick
on you, the message from Pid1 to Pid2 can arrive before the "startup"
message, and the program crashes. If you still want to do it this way
for some reason, you will have to establish two distinct phases.
As an example, I once had a simple program that set up a ring of
processes. It all worked well, until I upped the number of processes in
the ring over some limit (a couple of hundred) - at which time the
scheduler started running some of the first spawned processes before the
ring was completed. This would not have been a problem if the processes
had not been expecting an initialization message before they heard from
Richard Carlsson () (This space intentionally left blank.)
E-mail: WWW: http://user.it.uu.se/~richardc/
"Having users is like optimization: the wise course is to delay it."
-- Paul Graham
More information about the erlang-questions