Delphi Thread pool Queue

1.8k views Asked by At

Hi is it possible to find an example for the follow task. I am connecting on a Webscocket and i recive messages. after the message received i have to insert it to a database and i try to do it in a Queue Thread pool. at the moment i do the follow but i dont think is the corect way to do it.

           var MyStrings:Tstringlist;
             MyStrings:Tstringlist.create;


            //On Websocket message
           MyStrings.add(Message);


             //and in a Thread that always is Running i do 

      procedure TMythread.Execute;
      begin

      while true 
        begin

         if MyStrings.count>0 Then
         begin

          /////////////////////////////////////
          //database jobs 
          //with the string  MyStrings.strings(0)
         /////////////////////////////////////

         MyStrings.delete(0);
         end;


          end;
        end;

but this way i proccess the jobs one by one how is possible to have 10 threads at the same time to process the tasks and after the 10 threads finish to pop 10 more Strings from Mystrings if exists and process them to 10 seperate Threads?

1

There are 1 answers

1
Rafael On

First, you have to make your data exchange save. If you've a list which will be changed (add, remove entries) over different threads, you have to lock the resource.

For thread queue/pool have a look at below links:

http://docwiki.embarcadero.com/Libraries/Rio/en/System.Threading.TTask http://docwiki.embarcadero.com/Libraries/Rio/en/System.Classes.TThread.Queue