Thanks very much , its now pretty clear ~~<br>just didnt know the mechanism behind~~~<br><br><div><span class="gmail_quote">On 5/25/07, <b class="gmail_sendername">Ulf Wiger (TN/EAB)</b> <<a href="mailto:ulf.wiger@ericsson.com">
ulf.wiger@ericsson.com</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div>
<div><font color="#0000ff" face="Arial" size="2"></font> </div>
<div><span><font color="#0000ff" face="Arial" size="2">A
synchronous request is similar to</font></span></div>
<div><span><font color="#0000ff" face="Arial" size="2"></font></span> </div>
<div><span><font color="#0000ff" face="Arial" size="2">call(Server, Request) -></font></span></div>
<div><span><font color="#0000ff" face="Arial" size="2"> MonRef = erlang:monitor(process,
Server),</font></span></div>
<div><span><font color="#0000ff" face="Arial" size="2"> Msg = {'$gen_call', {self(), MonRef},
Request},</font></span></div>
<div><span><font color="#0000ff" face="Arial" size="2"> Server ! Msg,</font></span></div>
<div><span><font color="#0000ff" face="Arial" size="2"> receive</font></span></div>
<div><span><font color="#0000ff" face="Arial" size="2"> {MonRef, Reply} ->
Reply;</font></span></div>
<div><span><font color="#0000ff" face="Arial" size="2"> {'DOWN', MonRef, _, _, _} ->
erlang:error(...);</font></span></div>
<div><span><font color="#0000ff" face="Arial" size="2"> after 5000 -> </font></span></div>
<div><span><font color="#0000ff" face="Arial" size="2"> erlang:error(timeout</font></span></div>
<div><span><font color="#0000ff" face="Arial" size="2"> end.</font></span></div>
<div><span><font color="#0000ff" face="Arial" size="2"></font></span> </div>
<div><span><font color="#0000ff" face="Arial" size="2">(The
actual code is a little bit more contrived.)</font></span></div>
<div><span><font color="#0000ff" face="Arial" size="2"></font></span> </div>
<div><span><font color="#0000ff" face="Arial" size="2">A cast
is simply:</font></span></div>
<div><span><font color="#0000ff" face="Arial" size="2"></font></span> </div>
<div><span><font color="#0000ff" face="Arial" size="2">cast(Server, Msg) -></font></span></div>
<div><span><font color="#0000ff" face="Arial" size="2"> Server ! {'$gen_cast', Msg},</font></span></div>
<div><span><font color="#0000ff" face="Arial" size="2"> ok.</font></span></div>
<div><span><font color="#0000ff" face="Arial" size="2"></font></span> </div>
<div><span><font color="#0000ff" face="Arial" 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><font color="#0000ff" face="Arial" size="2"></font></span> </div>
<div><span><font color="#0000ff" face="Arial" 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><font color="#0000ff" face="Arial" size="2"></font></span> </div>
<div><span><font color="#0000ff" face="Arial" 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><font color="#0000ff" face="Arial" size="2"></font></span> </div>
<div><span><font color="#0000ff" face="Arial" size="2">Cast
is also asynchronous, while call isn't. Call gives a form of flow
control.</font></span></div>
<div><span><font color="#0000ff" face="Arial" size="2"></font></span> </div>
<div><span><font color="#0000ff" face="Arial" size="2">BR,</font></span></div>
<div><span><font color="#0000ff" face="Arial" size="2">Ulf
W</font></span></div><br>
<blockquote style="border-left: 2px solid rgb(0, 0, 255); padding-left: 5px; margin-left: 5px; margin-right: 0px;">
<div dir="ltr" align="left" lang="en-us">
<hr>
<font face="Tahoma" size="2"><b>From:</b> <a href="mailto:erlang-questions-bounces@erlang.org" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">erlang-questions-bounces@erlang.org</a>
[mailto:<a href="mailto:erlang-questions-bounces@erlang.org" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">erlang-questions-bounces@erlang.org</a>] <b>On Behalf Of </b>Ming
Zhao(Jimmy)<br><b>Sent:</b> den 25 maj 2007 14:46<br><b>To:</b>
<a href="mailto:erlang-questions@erlang.org" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">erlang-questions@erlang.org</a><br><b>Subject:</b> [erlang-questions] questions
about gen_server behaviour<br></font><br></div><div><span class="e" id="q_112c352bcc6ba63c_1">
<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" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">zhaoomingg@gmail.com</a><br>Tel: +46
(0)736964077<br>MSN:<br><a href="mailto:zhaoomingg@hotmail.com" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">zhaoomingg@hotmail.com</a><br>Skype:
zhaoomingg </span></div></blockquote></div>
</blockquote></div><br><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