How to make Haskell’s tchan delay mail like Erlang’s message queue?

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").

Output is:

Root finished
One
Two
Final
Spawn finished

The processing of the final message is basically delayed until the last receiving block because the previous receiving mode does not match the message

How did you do this with Haskell's tchan?

Solution

You mean Erlang's selective reception function As far as I know, Haskell's STM is parallel to this Your choice is to refactor your code to eliminate the need for it (for example, by using separate queues for different types of information that may be received) or to implement this function in the library

The semantics of selective reception is that there is a delayed message list in addition to the incoming message queue In the receive function, you need to scan the delay list first to get any matching messages If the message matches, delete it from the list and submit it If there is no delayed message match, you need to wait for the message in your inbox When you receive a message, you check for a match If so, you deliver it; If not, you push it to the postponement list and repeat

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>