But the data is sent only for the element with index 0. For others I get the error SIGSEGV Have I correctly stored the sockets from the client in TList?
Title: Re: TThread in TList. Acces to TTCPBlockSocket methods
Post by: Thaddy on August 09, 2018, 03:13:56 pm
Tlist is not threadsafe, although with care you can use it as you did. Use TThreadList from classes or TThreadlist<T> from rtl-generics (generics.collections.pas) The latter can greatly simplify your code as well. TThreadlist<TClientThread>; !! No more casts...
Also note specifically the last part of your code looks fishy to me: you are doing the sendstring from the context of the main thread, not from the threads. I suspect you need a redesign.
Title: Re: TThread in TList. Acces to TTCPBlockSocket methods
Post by: and_rivne on August 09, 2018, 07:59:41 pm
Thanks for the answer. Yes, I'm trying to send data from another class, to a higher level. I do not understand why:
Yes, I'm trying to send data from another class, to a higher level.
Although threads are classes (objects) and can have their own data and methods, they are not intended to be containers. A typical usage of threads is like a rocket: you setup it, you launch it and wait (or not) until they finish. They should do a job autonomously. Of course, a two-way communication is possible (with known limitations), but definitely you should not touch (directly) thread's internal data in the middle of the work.
Title: Re: TThread in TList. Acces to TTCPBlockSocket methods
Post by: and_rivne on August 10, 2018, 09:11:53 am
What are the options to save threads, except for the TList ? I did not understand how to get an element from a TThreadlist, and do not delete it.
Title: Re: TThread in TList. Acces to TTCPBlockSocket methods
Post by: taazz on August 10, 2018, 10:18:37 am
just a quick note. 1) the code you posted makes no sense what so ever. 2) the code you posted is too short to analyze the execution flow. 3) if the tlist that holds a list of threads is to be accessed from multiple threads then it is better to use a TThreadedList instead. At list that will give you some protection against list corruption.
other than that I need to see the execution method of TClientThread as well as the complete procedure of the loop you just posted before I can decide if the code you posted has any relevance to the problem you are having let alone pin point the problem it self.
Title: Re: TThread in TList. Acces to TTCPBlockSocket methods
Post by: and_rivne on August 10, 2018, 12:18:26 pm
Quote
other than that I need to see the execution method of TClientThread
Title: Re: TThread in TList. Acces to TTCPBlockSocket methods
Post by: sash on August 10, 2018, 01:31:02 pm
I'm very confused by your usage of terms client, server, thread, stream. It seems like your TClientThread is actually a Peer Thread on a server.
In thast case you should call (a response from server to client (browser) ) Socket.SendString('ok'); after or inside ProcessingData(S); as a part of your logical protocol.
Anyway, it seems like you're trying to implement a usual webserver (with Synapse). There are Synapse server demo, FPC's TFPHttpServer. Why don't you use it?
Title: Re: TThread in TList. Acces to TTCPBlockSocket methods
Post by: and_rivne on August 10, 2018, 02:13:41 pm
Yes you are right. This part of the code is similar to the operation of a web server. After the first request, the browser returns to the page. And this code is executed:
cln := TClientThread(ListenerClient.ThreadList.Items[0]).Socket.SendString('.... http body page....');
index=0 Then the browser sends requests for css and images in multiple threads simultaneously, which I save for return the result but here we get the error: