次の 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 の TChannel でこれを行うにはどうすればよいですか?