Why use send_after to send a message is reverse?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Why use send_after to send a message is reverse?

Dao Gui
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
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Why use send_after to send a message is reverse?

Hynek Vychodil
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 <[hidden email]> 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
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions



_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Why use send_after to send a message is reverse?

Jesper Louis Andersen-2
In reply to this post by Dao Gui

On Tue, Jun 9, 2015 at 1:32 PM, Dao Gui <[hidden email]> wrote:
this output is:
dddd:2
dddd:1

Not always! 

Suppose we have:

                erlang:send_after(1000,Pid, 1),
                timer:sleep(1), %% Simulate that we get broken here and other work has to be carried out
                erlang:send_after(1000,Pid,2).

Then the answer is

dddd:1
dddd:2

In other words, you better not rely on messages sent in the same time slot have the same ordering every time around. In R18, with multiple timer wheels, the situation is even more nondeterministic.

--
J.

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions