[erlang-questions] automatically delete older messages with the same tag (atom)?

Cameron Kerr ckerr@REDACTED
Wed May 6 01:23:43 CEST 2009

You just need a small proxy process that implements a message  
dictionary rather than a message queue. The dictionary could even be  
the processes dictionary. Something like this perhaps (completely  
untested, and from a relative newbie)

-export([get/2, start/0, stop/1]).

start() ->
	spawn(fun loop/0).  % should probably be spawn_link instead

loop() ->
		{get, From, Tag} ->
			From ! erlang:get(Tag),
			io:format("Returning message with tag ~p, value ~p and removing  
from dictionary~n",
					[Tag, erlang:get(Tag)]),
		stop ->
		{Tag, Msg}=M ->
			case erlang:put(Tag, Msg) of
				undefined ->
					io:format("Received message ~p; did not replace any old  
message~n", [M]);
				Old ->
					io:format("Received message ~p; replaced old message ~p~n", [M,  
		Other ->
			io:format("Received untagged message ~p~n", [Other]),

%%% Client functions

get(Pid, Tag) ->
	Pid ! {get, self(), Tag},
		Any -> Any

stop(Pid) ->
	Pid ! stop.

So basically all it does is implement a small process which accepts  
messages (eg. {msgtag, {some, [structure, of], [[whatever]]}}) and  
stores this in the processes dictionary, as illustrated with just put  
and get below:

1> put(foo, bar).
2> put(foo, baz).
3> get(foo).

Hopefully there are no glaring faults; I'm still reasonably new to  
designing programs in Erlang myself, perhaps others will help to point  
out and repair any holes.

On 06/05/2009, at 3:04 AM, Giorgos Kollias wrote:

> Hello Erlang users,
> Is there an option in the Erlang runtime to automatically delete a  
> message that has been deposited into a process's mailbox as soon as  
> a newer message with the same tag arrives? Is it easy to "patch" the  
> Erlang language source code to support this "little" feature in case  
> this is not there?
> Or the only feasible way of getting this behavior is just to receive  
> all messages till you get a sort of timeout and just keep the latest  
> message with the tag you are interested in (i.e. user code only) in  
> each receive phase? Is there a standard code idiom/snippet for this?
> This feature would be very interesting for implementing the  
> asynchronous iterations computing paradigm where each process only  
> keeps the most up-to-date message when it looks into its incoming  
> messages.
> Thanks in advance,
> giorgos
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions

Cameron Kerr <ckerr@REDACTED>
Teaching Fellow, Computer Science, University of Otago

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20090506/f36552ed/attachment.htm>

More information about the erlang-questions mailing list