Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2009-04

lNet and several threads

7 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Aleš Katona
to User
Show quoted text 5 lines
How I can use several threads with lNet visual library? I need some examples. Best regards!

Hello. lNet is inheritedly not thread-safe but it can be used with threads in some ways. When it comes to the visual components, you must remember that the main program loop (handled by the widgetsets) calls the lNet callbacks (OnReceive etc.) so they are all executed inside the main thread no matter where you work with the components otherwise.

I'd need some specific use case to give more info.

Ales

Reply by email

User
to Aleš Katona

Hello, I will explain better my doubt. I need to program a multithread server that processes requests of several clients at the same time. At the moment the server processes requests of several clients one to one by the OnReceive callback:

procedure TConnectionHandler.Receive(aSocket: TLSocket);
var
  Message: String;
begin
  if aSocket.GetMessage(Message) > 0 then
    MessagesManager.ProcessMessage(Message);
end;

I want to change the behavior of the server so that it works with one thread for each client request.

Is it possible?

Best regards.

2009/4/18 Ale? Katona

Show quoted text 23 lines
On Thu, 16 Apr 2009 20:01:57 +0200 User wrote: > How I can use several threads with lNet visual library? > > I need some examples. > > Best regards! Hello. lNet is inheritedly not thread-safe but it can be used with threads in some ways. When it comes to the visual components, you must remember that the main program loop (handled by the widgetsets) calls the lNet callbacks (OnReceive etc.) so they are all executed inside the main thread no matter where you work with the components otherwise. I'd need some specific use case to give more info. Ales _______________________________________________ Lazarus mailing list http://www.lazarus.freepascal.org/mailman/listinfo/lazarus

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.lazarus.freepascal.org/pipermail/lazarus/attachments/20090418/6438364e/attachment.html

Reply by email

Aleš Katona
to User
Show quoted text 45 lines
Hello, I will explain better my doubt. I need to program a multithread server that processes requests of several clients at the same time. At the moment the server processes requests of several clients one to one by the OnReceive callback: procedure TConnectionHandler.Receive(aSocket: TLSocket); var Message: String; begin if aSocket.GetMessage(Message) > 0 then MessagesManager.ProcessMessage(Message); end; I want to change the behavior of the server so that it works with one thread for each client request. Is it possible? Best regards. 2009/4/18 Ale? Katona > On Thu, 16 Apr 2009 20:01:57 +0200 > User wrote: > > > How I can use several threads with lNet visual library? > > > > I need some examples. > > > > Best regards! > > Hello. lNet is inheritedly not thread-safe but it can be used with threads > in some ways. When it comes to the visual components, you must remember that > the main program loop (handled by the widgetsets) calls the lNet callbacks > (OnReceive etc.) so they are all executed inside the main thread no matter > where you work with the components otherwise. > > I'd need some specific use case to give more info. > > Ales > _______________________________________________ > Lazarus mailing list > > http://www.lazarus.freepascal.org/mailman/listinfo/lazarus >

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.

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.

Ales

Reply by email

User
to Aleš Katona

Which is the simplest method?

2009/4/18 Ale? Katona

Show quoted text 73 lines
On Sat, 18 Apr 2009 10:36:04 +0200 User wrote: > Hello, I will explain better my doubt. I need to program a multithread > server that processes requests of several clients at the same time. At the > moment the server processes requests of several clients one to one by the > OnReceive callback: > > procedure TConnectionHandler.Receive(aSocket: TLSocket); > var > Message: String; > begin > if aSocket.GetMessage(Message) > 0 then > MessagesManager.ProcessMessage(Message); > end; > > I want to change the behavior of the server so that it works with one thread > for each client request. > > Is it possible? > > Best regards. > > 2009/4/18 Ale? Katona > > > On Thu, 16 Apr 2009 20:01:57 +0200 > > User wrote: > > > > > How I can use several threads with lNet visual library? > > > > > > I need some examples. > > > > > > Best regards! > > > > Hello. lNet is inheritedly not thread-safe but it can be used with threads > > in some ways. When it comes to the visual components, you must remember that > > the main program loop (handled by the widgetsets) calls the lNet callbacks > > (OnReceive etc.) so they are all executed inside the main thread no matter > > where you work with the components otherwise. > > > > I'd need some specific use case to give more info. > > > > Ales > > _______________________________________________ > > Lazarus mailing list > > > > http://www.lazarus.freepascal.org/mailman/listinfo/lazarus > > 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. 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. Ales _______________________________________________ Lazarus mailing list http://www.lazarus.freepascal.org/mailman/listinfo/lazarus

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.lazarus.freepascal.org/pipermail/lazarus/attachments/20090418/907e70c0/attachment-0001.html

Reply by email

Bogusław Brandys
to User

User pisze:

Which is the simplest method?

You can use synapse.It will be good for a multithreaded server if fpc thread support is stable and for just a few clients. It depends on what you want to archieve.

Boguslaw

Reply by email

User
to Bogusław Brandys

Ok, you know some examples?

Best regards.

2009/4/18 Bogus?aw Brandys

Show quoted text 16 lines
User pisze: > Which is the simplest method? You can use synapse.It will be good for a multithreaded server if fpc thread support is stable and for just a few clients. It depends on what you want to archieve. Boguslaw _______________________________________________ Lazarus mailing list http://www.lazarus.freepascal.org/mailman/listinfo/lazarus

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.lazarus.freepascal.org/pipermail/lazarus/attachments/20090418/58c3f561/attachment.html

Reply by email