[erlang-questions] Why use send_after to send a message is reverse?

Hynek Vychodil vychodil.hynek@REDACTED
Tue Jun 9 15:14:48 CEST 2015


Isn't it obvious? Appending head is O(1) operation, removing head is also
O(1) operation. Since there is not any guarantee in which order messages
should be returned, it is the obvious simplest solution. Your testing code
is very uncommon in a real application. There are usually two different
processes which schedule messages and the effect is unnoticeable because
their ordering is not defined.

On Tue, Jun 9, 2015 at 1:32 PM, Dao Gui <guidao1013@REDACTED> wrote:

> talk is cheap, show the code. ^_^
>
> -module(t).
> -export([main/0]).
> main() ->
>                 Pid = spawn(fun()->test2() end),
>                 erlang:send_after(1000,Pid, 1),
>                 erlang:send_after(1000,Pid,2).
> test2() ->
>
>                 receive
>                                 A ->
>                                         io:format("dddd:~p~n",[A]),
>                                         test2()
>                 end.
>
>
> --------------------------------------------------------------------------------------------------------
> this output is:
> dddd:2
> dddd:1
>
> then , I find this in time.c
>
> *    /* insert at head of list at slot */*
> *    p->next = tiw[tm];*
> *    p->prev = NULL;*
> *    if (p->next != NULL)*
> * p->next->prev = p;*
> *    tiw[tm] = p;*
>
> it insert head when we insert message
>
>
> */* Remove from list */*
> *remove_timer(p);*
> **timeout_tail = p; /* Insert in timeout queue */*
> *timeout_tail = &p->next;*
>
> it insert tail when it timeout.
>
>
> ----------------------------------------------------------------------------
> Q: but why ? if message timeout, we can insert head. it can keep the order
> of message. why don't do this?
>
> other: my English is poor. if something wrong, i hope you forgive me.
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20150609/4785b803/attachment.htm>


More information about the erlang-questions mailing list