[erlang-questions] questions about gen_server behaviour

Ulf Wiger (TN/EAB) ulf.wiger@REDACTED
Fri May 25 15:00:16 CEST 2007


 
A synchronous request is similar to
 
call(Server, Request) ->
   MonRef = erlang:monitor(process, Server),
   Msg = {'$gen_call', {self(), MonRef}, Request},
   Server ! Msg,
   receive
      {MonRef, Reply} -> Reply;
      {'DOWN', MonRef, _, _, _} -> erlang:error(...);
   after 5000 -> 
      erlang:error(timeout
   end.
 
(The actual code is a little bit more contrived.)
 
A cast is simply:
 
cast(Server, Msg) ->
   Server ! {'$gen_cast', Msg},
   ok.
 
So to answer your question, the gen_server sends the reply in the case
of a synchronous call.
 
The 'ok' in cast is not _sent_, it is simply the return value from the
cast/2 function.
 
With a call, you get actual acknowledgment from the server that the
request was received and processed. With cast, you have no such
information. The message may have been received and understood, but you
cannot know that.
 
Cast is also asynchronous, while call isn't. Call gives a form of flow
control.
 
BR,
Ulf W


________________________________

	From: erlang-questions-bounces@REDACTED
[mailto:erlang-questions-bounces@REDACTED] On Behalf Of Ming
Zhao(Jimmy)
	Sent: den 25 maj 2007 14:46
	To: erlang-questions@REDACTED
	Subject: [erlang-questions] questions about gen_server behaviour
	
	
	Could anyone explain these two functions below in details
(better with example)?
	1.synchronous request ----call
	Makes a synchronous call to the gen_server by sending a request
and waiting until a reply arrives or a timout occurs.
	who sends the reply? what exactly is the reply? 
	2.asynchronous request ----cast
	Sends an asynchronous request to the gen_server and returns ok
immediately
	who sends the ok? 
	
	these 2 functions seems to me the same ,
	so confused by them ..
	
	BR
	Thanks
	Jimmy
	
	-- 
	Ming   Zhao
	Dobelnsgatan 2C  231
	752 37 Uppsala, SWE
	
	Email: zhaoomingg@REDACTED
	Tel: +46 (0)736964077
	MSN:
	zhaoomingg@REDACTED
	Skype: zhaoomingg 

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


More information about the erlang-questions mailing list