[erlang-questions] Selective receive issue

Vlad Dumitrescu <>
Tue Aug 5 08:59:46 CEST 2008


Hi all,

I have an example of a receive where I don't seem to be able to use the
usual "dump any unrecognized messages" catch-all clause. I think it is also
an example where channels could be useful, but the main question is whether
I am missing the obvious or not.

I have a process that takes {input, Data} messages from its mailbox and
processes them (not necessarily one at a time). Flow control is implemented
as a pair of 'start'/'stop' messages. The way I implemented the main loop is
(simplified)

  receive
    start->Running = true, loop();
    stop->Running = false, loop()
  after 0 ->
      if Running -> process_one_step(), loop();
         true -> loop()
      end
  end,

where the process_one_step() function may call

  receive
    {input, Data} -> ...
  end

one or several times (it is configurable by the clients).

It seems to me that there is no place where I can put a '_->...;' clause
without creating problems. So I'd like to ask if anyone sees another way to
structure this that will allow rogue messages to be discarded.

------
A solution that requires an EEP :-) is to introduce an extension to the
receive statement that will explicitly  keep matched messages  in the
mailbox. For example

receive
  Pattern -> keep(), loop();
  Msg ->  process(Msg), loop()
end

keep() is the extension that says "leave the message in the mailbox and
proceed". This way process() will be called with all messages except those
matching Pattern

Would something like that be useful, or are there already ways to achieve
the same effect?

best regards,
Vlad
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080805/d5738d5d/attachment.html>


More information about the erlang-questions mailing list