Which is the simplest method?<br><br><div class="gmail_quote">2009/4/18 Aleš Katona <span dir="ltr"><<a href="mailto:almindor@gmail.com">almindor@gmail.com</a>></span><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
On Sat, 18 Apr 2009 10:36:04 +0200<br>
<div><div></div><div class="h5">User <<a href="mailto:usuarioanonimomysql@gmail.com">usuarioanonimomysql@gmail.com</a>> wrote:<br>
<br>
> Hello, I will explain better my doubt. I need to program a multithread<br>
> server that processes requests of several clients at the same time. At the<br>
> moment the server processes requests of several clients one to one by the<br>
> OnReceive callback:<br>
><br>
> procedure TConnectionHandler.Receive(aSocket: TLSocket);<br>
> var<br>
> Message: String;<br>
> begin<br>
> if aSocket.GetMessage(Message) > 0 then<br>
> MessagesManager.ProcessMessage(Message);<br>
> end;<br>
><br>
> I want to change the behavior of the server so that it works with one thread<br>
> for each client request.<br>
><br>
> Is it possible?<br>
><br>
> Best regards.<br>
><br>
> 2009/4/18 Aleš Katona <<a href="mailto:almindor@gmail.com">almindor@gmail.com</a>><br>
><br>
> > On Thu, 16 Apr 2009 20:01:57 +0200<br>
> > User <<a href="mailto:usuarioanonimomysql@gmail.com">usuarioanonimomysql@gmail.com</a>> wrote:<br>
> ><br>
> > > How I can use several threads with lNet visual library?<br>
> > ><br>
> > > I need some examples.<br>
> > ><br>
> > > Best regards!<br>
> ><br>
> > Hello. lNet is inheritedly not thread-safe but it can be used with threads<br>
> > in some ways. When it comes to the visual components, you must remember that<br>
> > the main program loop (handled by the widgetsets) calls the lNet callbacks<br>
> > (OnReceive etc.) so they are all executed inside the main thread no matter<br>
> > where you work with the components otherwise.<br>
> ><br>
> > I'd need some specific use case to give more info.<br>
> ><br>
> > Ales<br>
> > _______________________________________________<br>
> > Lazarus mailing list<br>
> > <a href="mailto:Lazarus@lazarus.freepascal.org">Lazarus@lazarus.freepascal.org</a><br>
> > <a href="http://www.lazarus.freepascal.org/mailman/listinfo/lazarus" target="_blank">http://www.lazarus.freepascal.org/mailman/listinfo/lazarus</a><br>
> ><br>
<br>
</div></div>Yes, but you need to do the sends inside the main loop. lNet is a state-machine so everything related to network states needs to be kept in the same thread. You can however start a new thread for each request and process the data there. When you have replies ready to send, push them in some send-buffer (per client) so that the main thread can do the sending then. This requires some sort of messaging between the threads of course.<br>
<br>
It's not the simplest of methods but it's safe. Doing sending inside the work-threads could cause lNet to run into a state-change race-condition and stop working properly.<br>
<div><div></div><div class="h5"><br>
Ales<br>
<br>
_______________________________________________<br>
Lazarus mailing list<br>
<a href="mailto:Lazarus@lazarus.freepascal.org">Lazarus@lazarus.freepascal.org</a><br>
<a href="http://www.lazarus.freepascal.org/mailman/listinfo/lazarus" target="_blank">http://www.lazarus.freepascal.org/mailman/listinfo/lazarus</a><br>
</div></div></blockquote></div><br>