My working environment is Erlang.
Can a process having 2 different functions have two receive blocks in the different functions.
receive
....
end.
request()
PID!(message)
%% Can i get the reply back here instead of the receive block above?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes, you can have many
receiveexpressions. When one is evaluated it will take the first matching message out of the message queue/mailbox (take your pick of names) and leave the rest for the nextreceive. Sending a message, using thePid ! Messagesyntax (the only way), is completely asynchronous and just adds the message to the end of the receiving processes message queue.receiveis the only way to receive messages i.e. take them out of the message queue. You can never put them back.There is no built-in synchronous message passing in Erlang, it is one by sending two messages:
The “requesting” process sends a message to the receiving process and then goes into a
receiveto wait for the reply.The “receiving” process will itself get the message in a
receive, process it, send the reply message back the the “requesting” process and then go into areceiveto sit and wait for the next message.Remember there is no inherent connection between processes, ever, and all communication is done using asynchronous message sending and
receive.So in reply to your second question: you can ONLY get a reply back in a
receiveexpression. That is the only way!Sorry to be a bit pedantic but Erlang doesn’t have either blocks or statements. It is a functional language and only has expressions which always return a value even if the return value is sometimes ignored.