title: Multithread queue in delphi?
body: <p>This is my second question about this, im having some troubles with this >.&lt;</p>
  
  <p>Well, I just want to create a limited number of threads (in this case, I want 10 threads), and then each thread will pick up a name in my list and get some data in my site.</p>
  
  <p>My system works pretty well, but my multi thread system still fails =(</p>
  
  <p>--</p>
  
  <p>I tried the code posted by LU RD, but the main thread don't wait the threads finish the queue, and just stops =(</p>
  
  <p>The code:</p>
  
  <pre><code>uses
  Classes,SyncObjs,Generics.Collections;
  
  Type
  TMyConsumerItem = class(TThread)
  private
   FQueue : TThreadedQueue&lt;TProc&gt;;
   FSignal : TCountDownEvent;
  protected
   procedure Execute; override;
  public
   constructor Create( aQueue : TThreadedQueue&lt;TProc&gt;; aSignal : TCountdownEvent);
  end;
  
  constructor TMyConsumerItem.Create(aQueue: TThreadedQueue&lt;TProc&gt;; aSignal : TCountDownEvent);
  begin
   Inherited Create(false);
   Self.FreeOnTerminate := true;
   FQueue := aQueue;
   FSignal := aSignal;
  end;
  
  procedure TMyConsumerItem.Execute;
  var
  aProc : TProc;
  begin
   try
   repeat
    FQueue.PopItem(aProc);
    if not Assigned(aProc) then
     break; // Drop this thread
    aProc();
   until Terminated;
   finally
    FSignal.Signal;
   end;
  end;
  
  procedure DoSomeJob(myListItems : TStringList);
  const
   cThreadCount = 10;
   cMyQueueDepth = 100;
  var
  i : Integer;
  aQueue : TThreadedQueue&lt;TProc&gt;;
  aCounter : TCountDownEvent;
  function CaptureJob( const aString : string) : TProc;
  begin
   Result :=
    procedure
    begin
      // Do some job with aString
    end;
  end;
  begin
  aQueue := TThreadedQueue&lt;TProc&gt;.Create(cMyQueueDepth);
  aCounter := TCountDownEvent.Create(cThreadCount);
  try
   for i := 1 to cThreadCount do
    TMyConsumerItem.Create(aQueue,aCounter);
   for i := 0 to myListItems.Count-1 do begin
    aQueue.PushItem( CaptureJob( myListItems[i]));
   end;
  finally
   for i := 1 to cThreadCount do
    aQueue.PushItem(nil);
   aCounter.WaitFor;  // Wait for threads to finish
   aCounter.Free;
   aQueue.Free;
  end;
  end;
  </code></pre>
  
  <p>My other question: <a href="https://stackoverflow.com/questions/15622261/multi-thread-delphi">Multi Thread Delphi</a></p>
  
  <p>Im using Delphi XE3.</p>