Pool of working threads?

Hi Experts,

Can you give me an example of 'Pool of working threads'?
My gues this is some amount of available threads which could be checked if they are free and then assigned a new task. The thread is not terminated after finished its task, but returned to the pool. This is the best I could explain what I am looking for.

Sincerely,
odissey1
LVL 2
odissey1Asked:
Who is Participating?
 
shaneholmesConnect With a Mentor Commented:
I don't think older versions of delphi have support for thread pooling. This is something you would have to implement on your own.

In the new Delphi,  new to the TThread class are a couple of methods. These methods are listed below:

    function WaitFor(TimeOut: Integer; var ReturnValue: LongWord): Boolean; overload;
    class procedure Queue(AThread: TThread; AMethod: TThreadMethod); overload;
    class procedure RemoveQueuedEvents(AThread: TThread; AMethod: TThreadMethod);
    class procedure StaticQueue(AThread: TThread; AMethod: TThreadMethod);

In previous versions of Delphi, the WaitFor() method did not allow to specify how long it should wait for the thread to terminate. TThread now adds another overloaded version of WaitFor() that provides a timeout parameter. This new version returns true when the thread terminates. Otherwise, if the timeout occurs, it returns false.

When a worker thread needs to execute a method from the main thread, the worker thread would block, waiting for the main thread to process the request. New to TThread, the Queue method provides the ability to asynchronously call methods in the main thread. By queuing the method instead of blocking, allows the worker thread to continue without waiting for the execution of the method placed on the queue. Eventually the main thread is ready to process the method(s) on the queue, and the entire queue is processed at once.

RemoveQueuedEvents() allows for either a specific method to be removed from the queue, or all of the queued methods for a specific thread to be removed.


ShaneHolmes
0
 
vadim_tiConnect With a Mentor Commented:
You can use suspend method in thread after hi finished its task.

And you can resume it or before terminating task or after you have a new task for it.

procedure TYourThread.Execute;
begin
  while not terminated do begin
     doSomeTask;
// here you can test if you have to much threads
// do not suspend and exit
    Suspend;
  end
end;

procedure TMainForm.NewTaskAvailable;
var
  done : boolean;
begin
  done := false;
  for i:=0 to ThreadList.Count - 1 do
     with TYourThread(ThreadList[i]) do
        if Suspended then begin
           Suspended := false;
          done := true;
           break;
        end
  if not done then
     ThreadList.Add(TYourThread.Create(true));
end;
0
 
shaneholmesCommented:
vadim_ti,

I believe what  odissey1 wants is a queue of threads. Where, one thread comes off the top, gets used, completes it completes its task, instead of getting terminated, it goes to the back of the queue waiting to be used again - then the next one comes off the top - and so on.

Is this  correct odissey1?
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
odissey1Author Commented:
To shaneholmes,

Yes, I think you describe it correctly. I've found some TWSocket example in the newsgroup and there it was advised to use a 'pool of working threads' (instead of regulal threading mechanism)  for a particular problem (boosts of high-speed data transfer with slow processing on receiving side). I decided to learn more about this approach and will be glad to get any extra knowlegde about that subject. The best would be some code snippet. I think I could follow from there.

Regards,
odissey1

 
0
 
shaneholmesCommented:
Would this be for some chat or P2P application?

ShaneHolmes
0
 
odissey1Author Commented:
Hi All,

I am sorry for delay. Thank you for help

Sincerely,
odissey1
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.