"Cleaning" the message queue

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

"Cleaning" the message queue

Vlad Dumitrescu-3
Hi all,

I have an application where processes get messages posted quite often, in a
periodical fashion. Sometimes they don't have time to finish processing one
message before another drops in, so the message queue grows longer and
longer.

What I'd want is that in case there are several such messages waiting, only
the latest shall be processed, while the rest shall be dumped. (The messages
are "virtual clock ticks")

I tried some schemes of setting up things, but none worked. It may be
supposed that there will only be one kind of messages in the queue.

What am I missing? Maybe there is another way to assure many processes get
common virtual time information? Where should I look?

Thanks in advance,
Vlad
_________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com.



Reply | Threaded
Open this post in threaded view
|

"Cleaning" the message queue

Willem Broekema
Heh, this looks like a nice way to compensate for my DOH get_tcp/gen_tcp
typo question. ;-)

Vlad Dumitrescu wrote:

> What I'd want is that in case there are several such messages waiting,
> only the latest shall be processed, while the rest shall be dumped.
> (The messages are "virtual clock ticks")

I came up with the following simple solution:

-module(getlatest).
-export([start/0]).

start() ->
    loop().

loop() ->
    receive
        {From, get_latest} ->
            From ! get_latest(),
            loop()
    end.

get_latest() ->
    get_latest(queue_was_empty).

get_latest(LastUntilNow) ->
%% if there's no new message in the queue, return 'LastUntilNow'
    receive
        Newer ->
            get_latest(Newer)
    after
        0 ->
            LastUntilNow
    end.


And works like:

23> S = spawn_link(getlatest, start, []).
<0.65.0>
24> S ! first.
first
25> S ! second.
second
26> S ! last.
last
27> S ! {self(), get_latest}.
{<0.41.0>,get_latest}
28> flush().
Shell got last
ok
29>

HTH

- Willem