<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=us-ascii">
<META content="MSHTML 6.00.2800.1589" name=GENERATOR></HEAD>
<BODY>
<DIV><FONT face=Arial color=#0000ff size=2></FONT> </DIV>
<DIV><SPAN class=727105412-25052007><FONT face=Arial color=#0000ff size=2>A
synchronous request is similar to</FONT></SPAN></DIV>
<DIV><SPAN class=727105412-25052007><FONT face=Arial color=#0000ff
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=727105412-25052007><FONT face=Arial color=#0000ff
size=2>call(Server, Request) -></FONT></SPAN></DIV>
<DIV><SPAN class=727105412-25052007><FONT face=Arial color=#0000ff
size=2> MonRef = erlang:monitor(process,
Server),</FONT></SPAN></DIV>
<DIV><SPAN class=727105412-25052007><FONT face=Arial color=#0000ff
size=2> Msg = {'$gen_call', {self(), MonRef},
Request},</FONT></SPAN></DIV>
<DIV><SPAN class=727105412-25052007><FONT face=Arial color=#0000ff
size=2> Server ! Msg,</FONT></SPAN></DIV>
<DIV><SPAN class=727105412-25052007><FONT face=Arial color=#0000ff
size=2> receive</FONT></SPAN></DIV>
<DIV><SPAN class=727105412-25052007><FONT face=Arial color=#0000ff
size=2> {MonRef, Reply} ->
Reply;</FONT></SPAN></DIV>
<DIV><SPAN class=727105412-25052007><FONT face=Arial color=#0000ff
size=2> {'DOWN', MonRef, _, _, _} ->
erlang:error(...);</FONT></SPAN></DIV>
<DIV><SPAN class=727105412-25052007><FONT face=Arial color=#0000ff
size=2> after 5000 -> </FONT></SPAN></DIV>
<DIV><SPAN class=727105412-25052007><FONT face=Arial color=#0000ff
size=2> erlang:error(timeout</FONT></SPAN></DIV>
<DIV><SPAN class=727105412-25052007><FONT face=Arial color=#0000ff
size=2> end.</FONT></SPAN></DIV>
<DIV><SPAN class=727105412-25052007><FONT face=Arial color=#0000ff
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=727105412-25052007><FONT face=Arial color=#0000ff size=2>(The
actual code is a little bit more contrived.)</FONT></SPAN></DIV>
<DIV><SPAN class=727105412-25052007><FONT face=Arial color=#0000ff
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=727105412-25052007><FONT face=Arial color=#0000ff size=2>A cast
is simply:</FONT></SPAN></DIV>
<DIV><SPAN class=727105412-25052007><FONT face=Arial color=#0000ff
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=727105412-25052007><FONT face=Arial color=#0000ff
size=2>cast(Server, Msg) -></FONT></SPAN></DIV>
<DIV><SPAN class=727105412-25052007><FONT face=Arial color=#0000ff
size=2> Server ! {'$gen_cast', Msg},</FONT></SPAN></DIV>
<DIV><SPAN class=727105412-25052007><FONT face=Arial color=#0000ff
size=2> ok.</FONT></SPAN></DIV>
<DIV><SPAN class=727105412-25052007><FONT face=Arial color=#0000ff
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=727105412-25052007><FONT face=Arial color=#0000ff size=2>So to
answer your question, the gen_server sends the reply in the case of a
synchronous call.</FONT></SPAN></DIV>
<DIV><SPAN class=727105412-25052007><FONT face=Arial color=#0000ff
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=727105412-25052007><FONT face=Arial color=#0000ff size=2>The
'ok' in cast is not _sent_, it is simply the return value from the cast/2
function.</FONT></SPAN></DIV>
<DIV><SPAN class=727105412-25052007><FONT face=Arial color=#0000ff
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=727105412-25052007><FONT face=Arial color=#0000ff size=2>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.</FONT></SPAN></DIV>
<DIV><SPAN class=727105412-25052007><FONT face=Arial color=#0000ff
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=727105412-25052007><FONT face=Arial color=#0000ff size=2>Cast
is also asynchronous, while call isn't. Call gives a form of flow
control.</FONT></SPAN></DIV>
<DIV><SPAN class=727105412-25052007><FONT face=Arial color=#0000ff
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=727105412-25052007><FONT face=Arial color=#0000ff
size=2>BR,</FONT></SPAN></DIV>
<DIV><SPAN class=727105412-25052007><FONT face=Arial color=#0000ff size=2>Ulf
W</FONT></SPAN></DIV><BR>
<BLOCKQUOTE
style="PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #0000ff 2px solid; MARGIN-RIGHT: 0px">
<DIV class=OutlookMessageHeader lang=en-us dir=ltr align=left>
<HR tabIndex=-1>
<FONT face=Tahoma size=2><B>From:</B> erlang-questions-bounces@erlang.org
[mailto:erlang-questions-bounces@erlang.org] <B>On Behalf Of </B>Ming
Zhao(Jimmy)<BR><B>Sent:</B> den 25 maj 2007 14:46<BR><B>To:</B>
erlang-questions@erlang.org<BR><B>Subject:</B> [erlang-questions] questions
about gen_server behaviour<BR></FONT><BR></DIV>
<DIV></DIV>Could anyone explain these two functions below in details (better
with example)?<BR>1.synchronous request ----call<BR>Makes a synchronous call
to the gen_server <CODE></CODE>by sending a request and waiting until a reply
arrives or a timout occurs.<BR>who sends the reply? what exactly is the reply?
<BR>2.asynchronous request ----cast<BR>Sends an asynchronous request to the
gen_server <CODE></CODE>and returns <CODE>ok</CODE> immediately<BR>who sends
the ok? <BR><BR>these 2 functions seems to me the same ,<BR>so confused by
them ..<BR><BR>BR<BR>Thanks<BR>Jimmy<BR clear=all><BR>-- <BR>Ming
Zhao<BR>Dobelnsgatan 2C 231<BR>752 37 Uppsala, SWE<BR><BR>Email: <A
href="mailto:zhaoomingg@gmail.com">zhaoomingg@gmail.com</A><BR>Tel: +46
(0)736964077<BR>MSN:<BR><A
href="mailto:zhaoomingg@hotmail.com">zhaoomingg@hotmail.com</A><BR>Skype:
zhaoomingg </BLOCKQUOTE></BODY></HTML>