Consider the following Erlang code:
-module(testit).
-export([testit/0]).
testit() ->
Pid = spawn(fun testit_proc/0),
Pid ! final,
Pid ! one,
Pid ! two,
io:format("Root finished~n").
testit_proc() ->
receive
one -> io:format("One~n");
two -> io:format("Two~n")
end,
receive
one -> io:format("One~n");
two -> io:format("Two~n")
end,
receive
one -> io:format("One~n");
two -> io:format("Two~n");
final -> io:format("Final~n")
end,
io:format("Spawn finished~n").
The output is:
Root finished
One
Two
Final
Spawn finished
The processing of the final
message is essentially deferred until the last receive block by virtue of the previous receive patterns not matching that message.
How do you do this with Haskell's TChan?
Chan
orTChan
packages -- you will have to implement a defer queue yourself. – Homophonous