<div>Hi, Eduardo<br> </div>
<div>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.
</div>
<div><br>I  skip first 112 of the error message<<112,131,104,3,97...>>, and use binary_to_term,get term like:<br><br> </div>
<blockquote class="gmail_quote" style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0.8ex; BORDER-LEFT: #ccc 1px solid">{2,'',<4761.49.0>}</blockquote>
<div> </div>
<div>I search the maillisting again, and only get one post about this:</div>
<div> </div>
<div><a href="http://www.erlang.org/pipermail/erlang-questions/2004-October/013383.html">http://www.erlang.org/pipermail/erlang-questions/2004-October/013383.html</a></div>
<div> </div>
<div>It looks like this problem is solved by using mutex<br><a href="http://www.erlang.org/pipermail/erlang-questions/2004-October/013385.html">http://www.erlang.org/pipermail/erlang-questions/2004-October/013385.html</a>
<br><br>But I still have this problem.</div>
<div> </div>
<div>Any help is appreciable!</div>
<div> </div>
<div>BR</div>
<div> </div>
<div>James</div>
<div> </div>
<div><br>2007/2/27, lang er <<a href="mailto:erlangist@gmail.com">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 href="http://eidefs.mk">eidefs.mk</a> file in the<br>> > erl interface src directory.<br>> <br>>  <br>> I checked the 
<a href="http://eidefs.mk">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 href="mailto:eduardo@inswitch.us">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 href="mailto:erlang-questions-bounces@erlang.org">erlang-questions-bounces@erlang.org</a> [mailto: <a href="mailto:erlang-questions-bounces@erlang.org">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 href="mailto:erlang-questions@erlang.org">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> </div>