I have a gen_server
and i do not understand the following:
-module(myserver).
-behaviour(gen_server).
-record(state,{ count=0}).
handle_cast(Message,From,State=#state{count=C})->
self() ! something,
{noreply,State}.
handle_info(Message,State=#state{count=C})
NewCount=case Message of
0 -> C+1;
true ->C,
{noreply,State#state{count=NewCount}}.
Given the above code i want to understand the following thing:
-assuming i enter handle_cast
and i send something to the mailbox
of the gen_server which fits the handle_info
case, will the handle_info
get called on a different thread right then and there ( self() ! Message
while i am still evaluating the handle_cast
?
What is the order in which things unfold ?
A
handle_cast
enterhandle_cast
send to own mailboxhandle_cast
returnshandle_info
gets triggeredB
handle_cast
enterhandle_cast
send to own mailboxhandle_info
gets triggered (concurrently) and may alter statehandle_cast
may or may not end before handle_info
finishes , and can get state altered by handle_info
The answer is A. All handlers of gen_server
- like handle_cast
or handle_info
- are always executed in the same single process. The message send to self within handle_cast
will be received by the gen_server
implementation after the handle_cast
callback returns. Only then handle_info
will be called, and handle_info
will receive the state returned from handle_cast
.
Generally, all gen_server
handlers are always called sequentially, and the state returned by a handler is passed to the handler which is called next.