<div>Hi <font face="Tahoma" size="2">Eduardo</font>, </div>
<div><br>After the previus modification failed, I also modify code as what you give.This time the normal status last for more time than before, </div>
<div>but eventually (about one hours later), I get this error again.</div>
<div> </div>
<div>The data transfered between Cnode and Erlang process is large(240k/second). I also run this program on a different machine, and use small</div>
<div>data (4k/second), it just work fine for more than 24 hours(and I exit it manually).</div>
<div> </div>
<div> </div>
<div>Thanks!</div>
<div><br>BR</div>
<div>James</div>
<div><span class="gmail_quote">2007/3/1, Eduardo Figoli (INS) <<a href="mailto:eduardo@inswitch.us">eduardo@inswitch.us</a>>:</span>
<blockquote class="gmail_quote" style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0.8ex; BORDER-LEFT: #ccc 1px solid">
<div lang="EN-US" vlink="blue" link="blue">
<div>
<p><font face="Arial" color="navy" size="2"><span style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial">Try</span></font></p>
<p><font face="Times New Roman" size="3"><span style="FONT-SIZE: 12pt">>  EnterCriticalSection(&CriticalSection);</span></font></p>
<p><font face="Times New Roman" size="3"><span style="FONT-SIZE: 12pt"><span class="q">> erlang_pid *channel_connection_pid = (erlang_pid *)dwUser;<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, pBuffer, dwBufSize);<br></span><span class="q">>  ei_send(acceptedFd, channel_connection_pid, 
xbuf.buff,xbuf.index);<br></span>>  ei_x_free(&xbuf);<br>>  LeaveCriticalSection(&CriticalSection);<br></span></font><font face="Arial" color="navy" size="2"><span style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial">
 </span></font></p>
<p><font face="Arial" color="navy" size="2"><span style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"> </span></font></p>
<p><font face="Arial" color="navy" size="2"><span style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial">Regards Eduardo</span></font></p>
<p><font face="Arial" color="navy" size="2"><span style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"> </span></font></p>
<div>
<div style="TEXT-ALIGN: center" align="center"><font face="Times New Roman" size="3"><span lang="ES" style="FONT-SIZE: 12pt">
<hr align="center" width="100%" size="2">
</span></font></div>
<p><b><font face="Tahoma" size="2"><span lang="ES" style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; FONT-FAMILY: Tahoma">De:</span></font></b><font face="Tahoma" size="2"><span lang="ES" style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma">
 lang er [mailto:<a onclick="return top.js.OpenExtLink(window,event,this)" href="mailto:erlangist@gmail.com" target="_blank">erlangist@gmail.com</a>] <br><b><span style="FONT-WEIGHT: bold">Enviado el:</span></b> Jueves, 01 de Marzo de 2007 05:41 
a.m.<br><b><span style="FONT-WEIGHT: bold">Para:</span></b> Eduardo Figoli (INS)<br><b><span style="FONT-WEIGHT: bold">CC:</span></b> <a onclick="return top.js.OpenExtLink(window,event,this)" href="mailto:erlang-questions@erlang.org" target="_blank">
erlang-questions@erlang.org</a><br><b><span style="FONT-WEIGHT: bold">Asunto:</span></b> Re: [erlang-questions] ei_send multithreaded problem</span></font><span lang="ES"></span></p></div>
<div><span class="e" id="q_1110da68d9818ff1_4">
<p><font face="Times New Roman" size="3"><span style="FONT-SIZE: 12pt"> </span></font></p>
<div>
<p><font face="Times New Roman" size="3"><span style="FONT-SIZE: 12pt">Hi, Eduardo<br> </span></font></p></div>
<div>
<p><font face="Times New Roman" size="3"><span style="FONT-SIZE: 12pt">I add a critical section before and after ei_send,now the code look like this:<br><br> <br>> erlang_pid *channel_connection_pid = (erlang_pid *)dwUser;
<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, pBuffer, dwBufSize);
<br>>  EnterCriticalSection(&CriticalSection);<br>>  ei_send(acceptedFd, channel_connection_pid, xbuf.buff,xbuf.index);<br>>  LeaveCriticalSection(&CriticalSection);<br>>  ei_x_free(&xbuf);<br>> 
<br><br><br>now the mormal running time is longer, but Erlang side still got "ERROR REPORT= Got invalid data on distribution channel, offending packet is:"  error eventually. </span></font></p></div>
<div>
<p><font face="Times New Roman" size="3"><span style="FONT-SIZE: 12pt"><br>I  skip first 112 of the error message<<112,131,104,3,97...>>, and use binary_to_term,get term like:<br><br> </span></font></p></div>

<blockquote style="BORDER-RIGHT: medium none; PADDING-RIGHT: 0cm; BORDER-TOP: medium none; PADDING-LEFT: 6pt; PADDING-BOTTOM: 0cm; MARGIN-LEFT: 4.8pt; BORDER-LEFT: #cccccc 1pt solid; MARGIN-RIGHT: 0cm; PADDING-TOP: 0cm; BORDER-BOTTOM: medium none">

<p><font face="Times New Roman" size="3"><span style="FONT-SIZE: 12pt">{2,'',<4761.49.0>}</span></font></p></blockquote>
<div>
<p><font face="Times New Roman" size="3"><span style="FONT-SIZE: 12pt"> </span></font></p></div>
<div>
<p><font face="Times New Roman" size="3"><span style="FONT-SIZE: 12pt">I search the maillisting again, and only get one post about this:</span></font></p></div>
<div>
<p><font face="Times New Roman" size="3"><span style="FONT-SIZE: 12pt"> </span></font></p></div>
<div>
<p><font face="Times New Roman" size="3"><span style="FONT-SIZE: 12pt"><a onclick="return top.js.OpenExtLink(window,event,this)" href="http://www.erlang.org/pipermail/erlang-questions/2004-October/013383.html" target="_blank">
http://www.erlang.org/pipermail/erlang-questions/2004-October/013383.html</a></span></font></p></div>
<div>
<p><font face="Times New Roman" size="3"><span style="FONT-SIZE: 12pt"> </span></font></p></div>
<div>
<p><font face="Times New Roman" size="3"><span style="FONT-SIZE: 12pt">It looks like this problem is solved by using mutex<br><a onclick="return top.js.OpenExtLink(window,event,this)" href="http://www.erlang.org/pipermail/erlang-questions/2004-October/013385.html" target="_blank">
http://www.erlang.org/pipermail/erlang-questions/2004-October/013385.html</a> <br><br>But I still have this problem.</span></font></p></div>
<div>
<p><font face="Times New Roman" size="3"><span style="FONT-SIZE: 12pt"> </span></font></p></div>
<div>
<p><font face="Times New Roman" size="3"><span style="FONT-SIZE: 12pt">Any help is appreciable!</span></font></p></div>
<div>
<p><font face="Times New Roman" size="3"><span style="FONT-SIZE: 12pt"> </span></font></p></div>
<div>
<p><font face="Times New Roman" size="3"><span style="FONT-SIZE: 12pt">BR</span></font></p></div>
<div>
<p><font face="Times New Roman" size="3"><span style="FONT-SIZE: 12pt"> </span></font></p></div>
<div>
<p><font face="Times New Roman" size="3"><span style="FONT-SIZE: 12pt">James</span></font></p></div>
<div>
<p><font face="Times New Roman" size="3"><span style="FONT-SIZE: 12pt"> </span></font></p></div>
<div>
<p><font face="Times New Roman" size="3"><span style="FONT-SIZE: 12pt"><br>2007/2/27, lang er <<a onclick="return top.js.OpenExtLink(window,event,this)" href="mailto:erlangist@gmail.com" target="_blank">erlangist@gmail.com
</a>>:<br>> <br>> But in the erl_interface document, it said:<br>>  <br>> <br>> > If you are using Erl Interface functions in a threaded application based on POSIX threads or Solaris <br>> > threads, then Erl Interface needs access to some of the synchronization facilities in your threads 
<br>> > package, and you will need to specify additional compiler flags in order to indicate which of the <br>> > packages you are using. Define REENTRANT and either STHREADS or PTHREADS. The default is to use
<br>> > POSIX threads if REENTRANT is specified. <br>> > Note that both single threaded and default versions of the Erl interface and Ei libraries are provided. <br>> > (The single threaded versions are named liberl interface st and libei st). Whether the default
<br>> > versions of the libraries have support for threads or not is determined by if the platform in question has <br>> > support for POSIX or Solaris threads. To check this, have a look in the <a onclick="return top.js.OpenExtLink(window,event,this)" href="http://eidefs.mk/" target="_blank">
eidefs.mk</a> file in the<br>> > erl interface src directory.<br>> <br>>  <br>> I checked the <a onclick="return top.js.OpenExtLink(window,event,this)" href="http://eidefs.mk/" target="_blank">eidefs.mk</a>
 file, its content is:<br>>  <br>>  <br>> <br>> <br>> <br>> # Have the ei and erl_interface libs been compiled for threads?<br>> EI_THREADS=true<br>> <br>> # Threads flags <br>> THR_DEFS=-DWIN32_THREADS -DUSE_DECLSPEC_THREAD
<br>> <br>> # Threads libs<br>> THR_LIBS=<br>> <br>> # ----------------------------------------------------------------------<br>> <br>> <br>> Doesn't it means I don't need use any synchronization facilities myself? 
<br>> <br>> Thanks!<br>> <br>> James<br>>  <br>> 2007/2/26, Eduardo Figoli (INS) <<a onclick="return top.js.OpenExtLink(window,event,this)" href="mailto:eduardo@inswitch.us" target="_blank">eduardo@inswitch.us
</a>>: <br>> <br>> > <br>> > <br>> > <br>> > <br>> > Have you tried using a critical section whenever calling ei_send?<br>> > <br>> >  <br>> > <br>> > Regards, Eduardo
<br>> > <br>> >  <br>> > <br>> > ________________________________ <br><br>> > <br>> > De: <a onclick="return top.js.OpenExtLink(window,event,this)" href="mailto:erlang-questions-bounces@erlang.org" target="_blank">
erlang-questions-bounces@erlang.org</a> [mailto: <a onclick="return top.js.OpenExtLink(window,event,this)" href="mailto:erlang-questions-bounces@erlang.org" target="_blank">erlang-questions-bounces@erlang.org </a>] En nombre de lang er
<br>> > Enviado el: Lunes, 26 de Febrero de 2007 01:05 p.m.<br>> > Para: <a onclick="return top.js.OpenExtLink(window,event,this)" href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org
</a><br>> > Asunto: [erlang-questions] ei_send multithreaded problem <br>> > <br>> > <br>> >  <br>> > <br>> > <br>> > 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: 
<br>> > <br>> >  <br>> > <br>> > --------------------------------------------------------------------<br>> > <br>> >  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);<br>> > <br>> > ----------------------------------------------------------------------<br>> > <br>> >   
<br>> > <br>> >  <br>> > <br>> > 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.) 
<br>> > <br>> >  <br>> > <br>> >  <br>> > <br>> > 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. 
<br>> > <br>> >  <br>> > <br>> > Do I miss anything to use ei_send in multithreaded environment?<br>> > <br>> >  <br>> > <br>> > compiler: ms visual studio 2005<br>> > 
<br>> > debug mode use  ei_mdd.lib <br>> > <br>> >  <br>> > <br>> >  <br>> > <br>> >  <br>> > <br>> > Thanks,<br>> > <br>> > James<br>> > <br>> >   
<br>> <br>> <br><br> </span></font></p></div></span></div></div></div></blockquote></div><br>