考虑以下 Erlang 代码:
-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").
输出是:
Root finished
One
Two
Final
Spawn finished
final
由于先前的接收模式与该消息不匹配,消息的处理基本上被推迟到最后一个接收块。
你如何用 Haskell 的 TChan 做到这一点?