[e-lang] [Fwd: Re: Proposal: E / Erlang integration]

Ulf Wiger (AL/EAB) ulf.wiger@REDACTED
Fri Jun 9 10:40:14 CEST 2006


 
David Hopwood wrote:
> 
> OTOH, I'm not entirely convinced that buffering messages 
> until we leave the waiting state (as the Erlang code 
> implicitly does) is exactly the right thing.
> For example, if we get an 'offhook' message, or if the 
> hardware fails, then the message buffer should be flushed. I 
> don't think there's any way to do that in Erlang (there is 
> with the receptionist approach).


The conventional way to implement flush() in Erlang is:

flush() ->
  receive
      _ -> flush()
  after 0 ->
      ok
  end.

If you want to be paranoid, you can insert a unique end token to prevent
a possible infinite loop:

flush() ->
  Ref = make_ref(),
  self() ! {end, Ref},
  flush_until({end, Ref}).

flush_until(Msg) ->
  receive
      Msg -> ok;
      _ -> flush_until(Msg)
  end.

There are of course variants of the above. Unfortunately, you can't pass
a pattern or parameterize the pattern match in receive. Partly for this
reason, there's no library function for dropping messages until a good
message comes along. You have to roll your own for the occasion.

Regards,
Ulf W



More information about the erlang-questions mailing list