<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:st1="urn:schemas-microsoft-com:office:smarttags" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv=Content-Type content="text/html; charset=us-ascii">
<meta name=Generator content="Microsoft Word 11 (filtered medium)">
<!--[if !mso]>
<style>
v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style>
<![endif]--><o:SmartTagType
namespaceuri="urn:schemas-microsoft-com:office:smarttags" name="PersonName"
downloadurl="http://www.microsoft.com"/>
<!--[if !mso]>
<style>
st1\:*{behavior:url(#default#ieooui) }
</style>
<![endif]-->
<style>
<!--
/* Font Definitions */
@font-face
{font-family:Tahoma;
panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
margin-bottom:.0001pt;
font-size:12.0pt;
font-family:"Times New Roman";}
a:link, span.MsoHyperlink
{color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{color:purple;
text-decoration:underline;}
span.EstiloCorreo17
{mso-style-type:personal-reply;
font-family:Arial;
color:navy;}
@page Section1
{size:612.0pt 792.0pt;
margin:72.0pt 90.0pt 72.0pt 90.0pt;}
div.Section1
{page:Section1;}
-->
</style>
</head>
<body lang=EN-US link=blue vlink=purple>
<div class=Section1>
<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size:
10.0pt;font-family:Arial;color:navy'>Have you tried using a critical section
whenever calling ei_send?<o:p></o:p></span></font></p>
<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size:
10.0pt;font-family:Arial;color:navy'><o:p> </o:p></span></font></p>
<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size:
10.0pt;font-family:Arial;color:navy'>Regards, Eduardo<o:p></o:p></span></font></p>
<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size:
10.0pt;font-family:Arial;color:navy'><o:p> </o:p></span></font></p>
<div>
<div class=MsoNormal align=center style='text-align:center'><font size=3
face="Times New Roman"><span lang=ES style='font-size:12.0pt'>
<hr size=2 width="100%" align=center tabindex=-1>
</span></font></div>
<p class=MsoNormal><b><font size=2 face=Tahoma><span lang=ES style='font-size:
10.0pt;font-family:Tahoma;font-weight:bold'>De:</span></font></b><font size=2
face=Tahoma><span lang=ES style='font-size:10.0pt;font-family:Tahoma'>
erlang-questions-bounces@erlang.org
[mailto:erlang-questions-bounces@erlang.org] <b><span style='font-weight:bold'>En
nombre de </span></b>lang er<br>
<b><span style='font-weight:bold'>Enviado el:</span></b> Lunes, 26 de Febrero
de 2007 01:05 p.m.<br>
<b><span style='font-weight:bold'>Para:</span></b> <st1:PersonName w:st="on">erlang-questions@erlang.org</st1:PersonName><br>
<b><span style='font-weight:bold'>Asunto:</span></b> [erlang-questions] ei_send
multithreaded problem</span></font><span lang=ES><o:p></o:p></span></p>
</div>
<p class=MsoNormal><font size=3 face="Times New Roman"><span lang=ES-UY
style='font-size:12.0pt'><o:p> </o:p></span></font></p>
<div>
<p class=MsoNormal style='background:white'><font size=3 face=Arial><span
style='font-size:12.0pt;font-family:Arial'>I'm writing a c node using ei layer.
This program is running under Window 2003 enterprise edition sp1, and
otp_win32_R11B-3. The following is a fragment of my code: </span></font><o:p></o:p></p>
<p class=MsoNormal style='background:white'><font size=3 face=Arial><span
style='font-size:12.0pt;font-family:Arial'> </span></font><o:p></o:p></p>
<p class=MsoNormal style='background:white'><font size=3 face=Arial><span
style='font-size:12.0pt;font-family:Arial'>--------------------------------------------------------------------</span></font><o:p></o:p></p>
<p class=MsoNormal style='background:white'><font size=3 face=Arial><span
style='font-size:12.0pt;font-family:Arial'> erlang_pid *pid = (erlang_pid
*)pUser;<br>
ei_x_buff xbuf;<br>
ei_x_new_with_version(&xbuf);<br>
ei_x_encode_tuple_header(&xbuf,2); <br>
ei_x_encode_atom(&xbuf,"data");<br>
ei_x_encode_binary(&xbuf, buffer, bufSize);<br>
ei_send(acceptedFd, pid, xbuf.buff,xbuf.index);<br>
ei_x_free(&xbuf);</span></font><o:p></o:p></p>
<p class=MsoNormal style='background:white'><font size=3 face=Arial><span
style='font-size:12.0pt;font-family:Arial'>----------------------------------------------------------------------</span></font><o:p></o:p></p>
<p class=MsoNormal style='background:white'><font size=3 face=Arial><span
style='font-size:12.0pt;font-family:Arial'> </span></font><o:p></o:p></p>
<p class=MsoNormal style='background:white'><font size=3 face=Arial><span
style='font-size:12.0pt;font-family:Arial'> </span></font><o:p></o:p></p>
<p class=MsoNormal style='background:white'><font size=3 face=Arial><span
style='font-size:12.0pt;font-family:Arial'>It is called inside a C
callback function and may be called by many concurrent threads. pUser include a
erlang_pid (correspond to a process in Erlang side).When some data come from network,
a third party library will invoke this callback function, so
ei_send pass these data to a corresponding Erlang process.(This
process is an Erlang equivalent to a 3rd library thread. So if I start one
process in Erlang side, the 3rd library will use one thread. If I start more
than one process in Erlang , the 3rd library will use same number of threads.) </span></font><o:p></o:p></p>
<p class=MsoNormal style='background:white'><font size=3 face=Arial><span
style='font-size:12.0pt;font-family:Arial'> </span></font><o:p></o:p></p>
<p class=MsoNormal style='background:white'><font size=3 face=Arial><span
style='font-size:12.0pt;font-family:Arial'> </span></font><o:p></o:p></p>
<p class=MsoNormal style='background:white'><font size=3 face=Arial><span
style='font-size:12.0pt;font-family:Arial'>Then, If I start one process in
Erlang side, everything is OK. But If I start more than one process, the C node
program will crash after some random time. If I comment out ei_send line
in above code fragment, everything is OK again. </span></font><o:p></o:p></p>
<p class=MsoNormal style='background:white'><font size=3 face=Arial><span
style='font-size:12.0pt;font-family:Arial'> </span></font><o:p></o:p></p>
<p class=MsoNormal style='background:white'><font size=3 face=Arial><span
style='font-size:12.0pt;font-family:Arial'>Do I miss anything to use ei_send in
multithreaded environment?</span></font><o:p></o:p></p>
<p class=MsoNormal style='background:white'><font size=3 face=Arial><span
style='font-size:12.0pt;font-family:Arial'> </span></font><o:p></o:p></p>
<p class=MsoNormal style='background:white'><font size=3 face=Arial><span
style='font-size:12.0pt;font-family:Arial'>compiler: ms visual studio 2005</span></font><o:p></o:p></p>
<p class=MsoNormal style='background:white'><font size=3 face=Arial><span
style='font-size:12.0pt;font-family:Arial'>debug mode use ei_mdd.lib </span></font><o:p></o:p></p>
<p class=MsoNormal style='background:white'><font size=3 face=Arial><span
style='font-size:12.0pt;font-family:Arial'> </span></font><o:p></o:p></p>
<p class=MsoNormal style='background:white'><font size=3 face=Arial><span
style='font-size:12.0pt;font-family:Arial'> </span></font><o:p></o:p></p>
<p class=MsoNormal style='background:white'><font size=3 face=Arial><span
style='font-size:12.0pt;font-family:Arial'> </span></font><o:p></o:p></p>
<p class=MsoNormal style='background:white'><font size=3 face=Arial><span
style='font-size:12.0pt;font-family:Arial'>Thanks,</span></font><o:p></o:p></p>
<p class=MsoNormal style='background:white'><font size=3 face=Arial><span
style='font-size:12.0pt;font-family:Arial'>James</span></font><o:p></o:p></p>
<p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size:
12.0pt'> <o:p></o:p></span></font></p>
</div>
</div>
</body>
</html>